如何设置具有不同列大小的Android GridView
我的主布局中有一个GridView,如下所示:如何设置具有不同列大小的Android GridView,android,android-layout,android-gridview,column-width,Android,Android Layout,Android Gridview,Column Width,我的主布局中有一个GridView,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</LinearLayout>
这样就产生了这样的网格
每个单元格的内容都是这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/btn_measurement" >
<Button
android:id="@+id/numerical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="---" />
</LinearLayout>
但我希望第四栏是其他栏的三分之一,我就是做不到
我查看了以下链接:
及
但是他们没有帮助(
如果你认为我需要完全改变我的解决方案,请不要只是给出一般性的线索。
Thanx使用此代码使列的大小变小,但这里您必须应用一些逻辑。第4列您希望它变小,因此您必须重写getView()方法,然后使用一些变量并检查该变量是否为3、7或11…然后将按钮的大小变小。我想您明白我的意思了
int logic =3;
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
LinearLayout layout;
if (convertView == null) {
convertView = inflate the layout and initialize convertview
if(position % logic==0)
converView..setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT,
(float) 0.3));
btn = new Button(context);
btn.setGravity(Gravity.RIGHT);
btn.setPadding(5, 5, 5, 5);
layout = new LinearLayout(context);
layout.setOrientation(0);
layout.setPadding(5, 5, 5, 10);
btn.setText(names[position]);
layout.addView(btn);
}
else {
layout = (LinearLayout) convertView;
btn = (Button) layout.getChildAt(0);
btn.setText(names[position]);
}
return layout;
}
我曾经使用过这个解决方案 通过使用列表视图而不是网格视图,并为每个项目在一个水平线性布局中放置4个按钮。但是您需要额外的工作来检测按钮单击,但我认为这是可行的 我能找到的另一个工作是使用第三方库,比如这个 使用带有自定义GridLayoutManager的RecyclerView(而不是GridView),如“可变跨度大小”下所示: 基本上,每个常规列占用3x,较小的列占用1x
GridLayoutManager manager = new GridLayoutManager(this, 10); // 3 cols of 3 + 1 col of 1
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position % 4 == 3)
return 1; // Fourth column is 1x
else
return 3; // Remaining columns are 3x
}
});
recyclerView.setLayoutManager(manager);
好的,这会减小标题文本视图和第4列中按钮的大小,但不会具体减小第4列的大小。这意味着只有按钮和文本视图会缩小,而不是列。好的,你是对的,但现在你可以通过设置layoutparamsconverView..setLayoutParams对converView执行此操作(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,(float)0.3));在这一行
covertView
为空,因此显然它会在这里给出一个异常。如何处理它?您检查/调试它,并看到..它不是空的。converView..setLayoutParams(new LinearLayout.layout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,(float)0.3));在这一行,covertView为null,因此显然它会在这里给出一个异常。如何处理它?它会引发null指针异常。您需要交错视图。请参阅\