【问题标题】:Variable width GridView according to number of cells根据单元格数量可变宽度 GridView
【发布时间】:2016-08-21 14:31:43
【问题描述】:

继续这个问题Layout dynamic grid in middle

我有一个网格。它具有不同的宽度,具体取决于用户的操作。有时可能是 4 * 3,有时可能是 2 *5

宽度也可以大于4。

我的问题是网格本身总是相同的宽度,并且单元格会拉伸以填充它。这意味着 2 * 5 的单元格宽度是 4 * 3 单元格宽度的两倍。理想情况下,我希望调整网格宽度。它应该只是 columnWidth * numOfColumns。

我下面的 xml 的宽度为 175px,我认为这是造成这种情况的原因。当我将其设为 wrap_content 时,它会占据整个屏幕宽度并忽略我的两个填充视图。

有人可以帮忙吗? 谢谢

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#C8C8C8"
        android:orientation="horizontal">

    <View
            android:background="#C8C8C8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/grid_view"
              android:background="#FF000000"
              android:layout_width="175px"
              android:layout_height="wrap_content"
              android:numColumns="5"
              android:columnWidth="35dp"
              android:verticalSpacing="1dp"
              android:horizontalSpacing="1dp"
            />
    <!--android:layout_width="wrap_content"-->

    <View
            android:background="#FFC8C8C8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
</LinearLayout>

再澄清一下我是什么。

上半部分包含一个网格,理想情况下应该居中。 单元格应该是相同的宽度(理想情况下是相同的高度,所以是正方形的) 上面的 LinearLayout 下有一个列表,如果网格占用太多空间(但绝不应占用超过 50% 的屏幕),该列表应该会变小

【问题讨论】:

  • 它应该只是 columnWidth x numOfColumns。 - 但是您认为列宽的值是多少?
  • 我在考虑每个宽度 30-50dp
  • 您不能直接在布局中执行此操作,您需要计算这些值并手动设置它们。我会使用自定义 ViewGroup 来保存 GridView 和 ListView 并在自定义 ViewGroup 中计算确切的值。

标签: android android-layout gridview layout android-linearlayout


【解决方案1】:

您需要将grid_view的宽度设置为wrap_contentcenter_horizontal = "true",而不是制作适配器,并在getView()方法中将单元格宽度和高度设置为固定值(从dp计算),之后当你将更改代码中的单元格数量 - 网格视图将更改其宽度以适合单元格数量 * 每个单元格的宽度

//cellWidth = width of each cell including paddings
//gridRowsNumber = number of cells in width
//gridColsNumber = number of cells in height
// in activity's of fragment's onCreate();
gridView.getLayoutParams().width = cellWidth * gridRowsNumber;
gridView.getLayoutParams().height = cellWidth * gridColsNumber;

在布局中

<GridView
    android:id="@+id/grid_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white"
    android:numColumns="5"
    android:scrollbars="none">
</GridView>

在适配器中

public View getView (int position, View convertView, ViewGroup parent) 
{
    ...
    holder.cellView.getLayoutParams().height = cellWidth;
    holder.cellView.getLayoutParams().width = cellWidth;
    ...
}

【讨论】:

  • 太棒了。效果很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2019-07-11
相关资源
最近更新 更多