2016-05-04 4 views
2

Я создал сетку с двумя столбцами и тремя строками, содержащими изображения с текстом. enter image description hereАвторежим размера сетки?

Everthing прекрасно работает .. Но проблема в том, когда я пытаюсь с другим видом экрана ... У меня есть Moto X 2013 и изображения имеют 192x192 точек и GridView выглядит великолепно

С Moto G2 у меня есть свиток в де GridView и я не хочу

Я положил это размеры для изображения в respectives Drawable папки: xxhdpi - 240х240 РХ xhdpi - 192x192 точек ИПЧР - 144x144 точек mdpi - 92x92 px

И у меня такая же проблема. Почему этот размер для телефона? Поскольку я хочу, чтобы gridview выглядел как картинка, заполните высоту (поэтому я стараюсь с разным размером знать, как правильно для моего телефона), но Moto G2 разочаровывает меня. Если я установил размер изображения меньше 192x192, то сетка не будет заканчиваться в нижней части экрана.

Это мой

grid_item.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="#455A64" 
android:gravity="center_horizontal"> 
<ImageView 
    android:id="@+id/picture" 
    android:layout_marginTop= "10dp" 
    android:layout_marginBottom="10dp" 
    android:layout_gravity="center_horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</ImageView> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:layout_gravity="center_horizontal"/> 

</LinearLayout> 

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:fitsSystemWindows="true"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="start" 
    android:paddingTop="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:background="@color/primary_dark" 
     android:weightSum="1"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_margin="8dp" 
      android:layout_height="match_parent" 
      android:background="@drawable/style_spinner" 
      android:weightSum="1"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="Ciudad: " 
       android:paddingLeft="6dp" 
       android:id="@+id/textView34" 
       android:textAlignment="center" 
       android:layout_gravity="center_vertical" 
       android:textSize="18dp" /> 

      <Spinner 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 

       android:layout_marginRight="8dp" 
       android:id="@+id/spn_ciudad" 
       android:layout_weight="0.95" /> 

     </LinearLayout> 


    </LinearLayout> 

    <GridView 
     android:id="@+id/gridViewMainActivity" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|center_vertical" 
     android:verticalSpacing="1dp" 
     android:horizontalSpacing="1dp" 
     android:background="@color/primary_dark" 
     android:stretchMode="columnWidth" 
     android:numColumns="2"/> 



</LinearLayout> 


</RelativeLayout> 

GridViewAdapter.java

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class GridViewAdapter extends BaseAdapter { 
private final List<Item> mItems = new ArrayList<Item>(); 
private final LayoutInflater mInflater; 

public GridViewAdapter (Context context) { 
    mInflater = LayoutInflater.from(context); 
    mItems.add(new Item("Buscar por complejos",  R.drawable.clock)); 
    mItems.add(new Item("Buscar por horarios", R.drawable.clock)); 
    mItems.add(new Item("Buscar por localizacion", R.drawable.clock)); 
    mItems.add(new Item("Ver ultimas operaciones",R.drawable.clock)); 
    mItems.add(new Item("Green",R.drawable.clock)); 
    clock.add(new Item("Cyan",  R.drawable.clock)); 
} 

@Override 
public int getCount() { 
    return mItems.size(); 
} 

@Override 
public Item getItem(int i) { 
    return mItems.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return mItems.get(i).drawableId; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    View v = view; 
    ImageView picture; 
    TextView name; 

    if (v == null) { 
     v = mInflater.inflate(R.layout.grid_item, viewGroup, false); 
     v.setTag(R.id.picture, v.findViewById(R.id.picture)); 
     v.setTag(R.id.text, v.findViewById(R.id.text)); 
    } 

    picture = (ImageView) v.getTag(R.id.picture); 
    name = (TextView) v.getTag(R.id.text); 

    Item item = getItem(i); 

    picture.setImageResource(item.drawableId); 
    name.setText(item.name); 

    return v; 
} 

private static class Item { 
    public final String name; 
    public final int drawableId; 

    Item(String name, int drawableId) { 
     this.name = name; 
     this.drawableId = drawableId; 
    } 
} 
} 

Это как Moto G2 выглядит (Do вы видите прокрутку ba г ?, и если экран меньше, чем G2 Moto, свиток гораздо больше): enter image description here

Любое решение для моей проблемы? Спасибо за помощь!

ответ

0

Попробуйте добавить android: numColumns = "auto_fit" и android: stretchMode = "columnWidth" и установка родительской высоты GridView в MATCH_PARENT.

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" /> 

Пример код из http://developer.android.com/intl/es/guide/topics/ui/layout/gridview.html

+0

Не работает :(, и мне нужно два столбца, не больше, чем –

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