我创建了一个包含2列和3行的gridview,其中包含带文本的图像。
Everthing效果很好..但问题是当我尝试使用其他类型的屏幕时...... 我有一个Moto X 2013,图像有192x192像素,gridview看起来很棒
使用moto G2,我有一个滚动到de gridview和我不想要它
我将图片的大小放在相应的可绘制文件夹中: xxhdpi - 240x240像素 xhdpi - 192x192 px hdpi - 144x144 px 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的样子(你看到滚动条了吗?如果屏幕小于Moto G2,那么滚动就更多了!):
解决我的问题的任何解决方案?谢谢你的帮助!
答案 0 :(得分:0)
尝试添加android:numColumns =&#34; auto_fit&#34;和android:stretchMode =&#34; columnWidth&#34;并将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
的示例代码