Android的GridView控件调节屏幕尺寸 [英] Android gridview adjusting to screen size
问题描述
我的应用程序的网格视图中有3行3列。我想这填满屏幕,而不管设备的屏幕大小。
The grid view of my application has 3 rows and 3 columns. I want this to fill the screen ,irrespective of the screen size of the device.
我试图让窗口的大小,并相应设置的LayoutParams。但是,这是不是给一个精确对准,因为它的工作原理与weightsum在LinearLayout中。
I tried getting window size and setting the layoutparams accordingly. But this is not giving a perfect alignment as it works with weightsum in linearlayout.
所以,我们可以使用weightsum的GridView的或有可用于任何其他Android财产。 非常感谢您的时间和响应。
So can we use weightsum for gridview or is there any other android property that can be used. Thanks a lot for time and response.
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/text1view"
android:background="@drawable/home_bg"
android:gravity="center"
android:horizontalSpacing="10dip"
android:numColumns="3"
android:stretchMode="columnWidth" />
每个网格产品
each grid item is
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="75dip"
android:layout_margin="0dip"
android:padding="0dip" >
</ImageView>
<TextView
android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/grid_item_image"
android:gravity="center_horizontal"
android:padding="0dip"
android:textColor="#000000"
android:textSize="10sp" >
</TextView>
$ C $适配器C:
code of the adapter :
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
// holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
convertView.setLayoutParams(new GridView.LayoutParams(h - 45,
h - 39));
// holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
// holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
// holder.image.setPadding(20, 20, 20, 20);
// convertView.setLayoutParams(new GridView.LayoutParams(Utils
// .getLayoutParameter(), Utils.getLayoutParameter()+50));
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new ModuleManager().startModuleACtivity(position, mContext);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
}
请参见行
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
我来到这个值对不同大小的仿真器的一些试验。 但我不认为它是一种正确的方式来定义的高度和宽度。
i arrived at this value with some trials on different size emulator. But i dont think its a right way to define height and width.
推荐答案
据我理解你的问题,因为你还没有发布任何code,你正在尝试做的是,你要GridView控件出现在全屏。
As far as i understood your question, as you have not posted any code, what you are trying to do is, you want gridview to appear on full screen.
首先,你必须使用一些布局,把你的GridView中执行以下操作:
Firstly you must be using some layout to put your gridview in. Do the following:
请高度和宽度的布局,以填补父:
Make height and width of the layout to fill parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
然后,设置高度和宽度你的GridView来FILL_PARENT为好。
then, set height and width of your gridview to fill_parent as well.
好了,这是你可能会一直这样只是一个假设的解决方案。把一些code就会好很多。希望这对你的作品。
Well, this is just a supposed solution that you might have been working this way. putting some code would be much better. Hope this works for you.
更新: 那么问题可能在于这里:
Update: Well the problem may lie here:
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
更改为:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
convertView.setLayoutParams(new GridView.LayoutParams(params));
希望这对你的作品。
Hope this works for you.
这篇关于Android的GridView控件调节屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!