当设备大小超过5英寸时,如何以编程方式更改Gridview 'android:numColumns="2"'
或如何在平板电脑上检测活动创建并设置'numColumns=3'
。
感谢。
答案 0 :(得分:2)
将numColums设置为auto_fit
以显示尽可能多的列以填充可用空间。
答案 1 :(得分:1)
在这种情况下你可以做的是为大于5英寸的设备创建两种不同的布局,在这些布局中设置屏幕空间小于5英寸的设备,你可以根据需要设置XML的列数。
答案 2 :(得分:1)
试试这个:
<GridView
android:id="@+id/gridView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
>
</GridView>
并在.java文件中
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
int width = 0, height = 0;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
if(width >480 && width <780)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(3);
if(density <200)
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
}
else if(width<=480)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(3);
h=150;
w=150;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else if(width >= 780)
{
setContentView(R.layout.home_student);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setNumColumns(4);
if(density <200)
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
else
{
h=200;
w=200;
CustomAdapterStudent mAdapter;
mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w);
gridView.setAdapter(mAdapter);
}
}
希望这可能会对你有帮助!