2016-09-14 3 views
0

Мне нужны изображения размером с контейнер «GridLayout» в соответствии с размером.Изменение размера сетки с вертикальными изображениями - андроид

Просто поймайте их горизонтальный размер, но мне также нужна вертикаль.

Что мне не хватает? благодаря

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@mipmap/fondo_01"> 


<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:useDefaultMargins="false" 
    android:background="@drawable/corners_main_layout" 
    android:animationCache="false" 
    android:layout_margin="@dimen/padding_white" 
    tools:layout_margin="@dimen/padding_white" 
    android:columnOrderPreserved="false" 
    android:columnCount="3"> 


    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout"> 
     <ImageView 
      android:src="@drawable/ic_skinav" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idskinav" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/skinav" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout9"> 
     <ImageView 
      android:src="@drawable/ic_social" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idsocial" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/social" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout8"> 
     <ImageView 
      android:src="@drawable/ic_stats" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idstats" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/stats" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout10" 

     > 
     <ImageView 
      android:src="@drawable/ic_alerts" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idalerts" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/alerts" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout2"> 
     <ImageView 
      android:src="@drawable/ic_inbox" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idinbox" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/inbox" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:orientation="vertical" 
     android:layout_columnWeight="1" 
     android:id="@+id/linearLayout3"> 
     <ImageView 
      android:src="@drawable/ic_info" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/idinfo" /> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/info" 
      android:layout_gravity="center" 
      android:textColor="@android:color/black" 
      /> 
    </LinearLayout> 

</GridLayout> 


</LinearLayout> 

Есть еще более простой способ сделать это? Я попытался с новым ConstraintLayout, но мне это не удалось.

------ ОБНОВЛЕНИЕ ----

enter image description here

ответ

1

Вы можете использовать квадратное изображение View для правильного определения размеров ваших грид предметов.

Создать отдельный класс для определения размера динамически для ImageView, который должен наследовать класс ImageView, как показано ниже:

public class SquareImageView extends ImageView { 

    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 

    int width = getMeasuredWidth(); 
     setMeasuredDimension(width, width); 

    } 
} 

В setMeasuredDimension (ширина, ширина); автоматически установит высоту как ширину.

В XML-файле использовать этот класс как вид вместо ImageView, как показано ниже:

<com.packagepath.SquareImageView 
    android:id="@+id/Imageview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
+0

Привет, спасибо за ответ, РАБОТАЕТ !!! благодаря!! – daicon

+1

Если контейнер представляет собой меньшую сетку, тогда вам нужно сделать соотношение соответствующим образом. Размер контейнера, который вы даете, требует по меньшей мере 4 в каждом ряду, чтобы он выглядел прилично. Я сам столкнулся с подобными ситуациями. Надеюсь, я убедился. –

+0

Теперь у меня есть еще одна небольшая проблема ..... Какая альтернатива для android layout_columnWeight = "1"? API работает только с> 21 .... мне нужно> 16 – daicon

Смежные вопросы