Java 如何自动调整GridView的大小
如何使GridView自动调整为屏幕分辨率。我在虚拟设备上看起来不错,但在手机上看不到。我尝试了很多设置,但都不起作用Java 如何自动调整GridView的大小,java,android,android-layout,gridview,Java,Android,Android Layout,Gridview,如何使GridView自动调整为屏幕分辨率。我在虚拟设备上看起来不错,但在手机上看不到。我尝试了很多设置,但都不起作用 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="f
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:stretchMode="columnWidth"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="10dp"
android:gravity="center_vertical"
android:layout_marginBottom="100dp"/>
</RelativeLayout>
虚拟设备:
电话
是200dx,不是dp,这就是原因。
因此,在Values文件夹中创建dimensions.xml,如
<resources>
<dimen name="gridview">200dp</dimen>
</resources>
是200dx,不是dp,这就是原因。
因此,在Values文件夹中创建dimensions.xml,如
<resources>
<dimen name="gridview">200dp</dimen>
</resources>
问题不在于gridview,而在于imageview本身 如您所见,网格实际上占用了屏幕,并用columnswidth填充了额外的空间。在您的情况下,您必须将图像放在中间。为此,请为imageview指定一个父对象,例如linearlayout,并将imageview与父对象中的中心对齐
LinearLayout lLayout=new LinearLayout(context);
lLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lLayout.setGravity(Gravity.CENTER);
lLayout.addView(<your imageview>);
问题不在于gridview,而在于imageview本身 如您所见,网格实际上占用了屏幕,并用columnswidth填充了额外的空间。在您的情况下,您必须将图像放在中间。为此,请为imageview指定一个父对象,例如linearlayout,并将imageview与父对象中的中心对齐
LinearLayout lLayout=new LinearLayout(context);
lLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lLayout.setGravity(Gravity.CENTER);
lLayout.addView(<your imageview>);
你能帮我写些代码吗?不知道在哪里添加这个代码。你能帮我写一些代码吗?不知道在哪里添加此代码。
LinearLayout lLayout=new LinearLayout(context);
lLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lLayout.setGravity(Gravity.CENTER);
lLayout.addView(<your imageview>);
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
LinearLayout lLayout;
if (convertView == null) {
// if it's not recycled, initialize some attributes
lLayout=new LinearLayout(context);
lLayout.setLayoutParams(new LinearLayout.LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lLayout.setGravity(Gravity.CENTER);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
lLayout.addView(imageView);
} else {
lLayout= (LinearLayout) convertView;
}
((ImageView)lLayout.getChildAt(0)).setImageResource(R.drawable.card_back);
return lLayout;
}