Android GridLayout列宽

Android GridLayout列宽,android,android-layout,android-gridlayout,Android,Android Layout,Android Gridlayout,我的GridLayout中有两列。我想做的是让这些列占据屏幕宽度的一半,然后让它们的子内容填充它们自己的单元格宽度/高度。我尝试将子项设置为fill\u parent,但这只会导致第一个子项接管整个布局。而且GridLayout似乎不支持权重?也许有更好的布局可以使用,但我想要一个网格样式的布局,这样看起来才是自然选择。好的,所以我放弃了网格视图,只使用了一些线性布局。我做了一个垂直的,然后添加了两个水平。它比网格视图稍微复杂一些。。。但在我弄明白之前,至少这是可行的 您可以扩展Relati

我的
GridLayout
中有两列。我想做的是让这些列占据屏幕宽度的一半,然后让它们的子内容填充它们自己的单元格宽度/高度。我尝试将子项设置为
fill\u parent
,但这只会导致第一个子项接管整个布局。而且GridLayout似乎不支持权重?也许有更好的布局可以使用,但我想要一个网格样式的布局,这样看起来才是自然选择。

好的,所以我放弃了网格视图,只使用了一些线性布局。我做了一个垂直的,然后添加了两个水平。它比网格视图稍微复杂一些。。。但在我弄明白之前,至少这是可行的


您可以扩展RelativeLayout类(如果愿意,也可以使用LinearLayout),以确保项目的高度与高度相同

public class GridItem extends RelativeLayout {

        public GridItem(Context context) {
                super(context);
        }

        public GridItem(Context context, AttributeSet attr) {
                super(context, attr);
        }

        public GridItem(Context context, AttributeSet attr, int integer) {
                super(context, attr, integer);
        }

        // Override onMeasure to give the view the same height as the specified width
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
        }

}
项目布局的父视图应为GridItem视图,以确保其正常工作。这必须是要在ListAdapter的getView中充气的布局文件

<?xml version="1.0" encoding="utf-8"?>
<my.packagename.GridItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The content of the item -->

</my.packagename.GridItem>

并将GridView的stretchMode设置为columnWidth。这将使所有项目适合指定的列数宽度。新视图将确保它们也具有相同的高度

<GridView
    android:id="@+id/gridList"
    android:numColumns="2"
    android:stretchMode="columnWidth"
/>

此代码在API21之前的支持库中提供

我有一段简单的代码,可以在2列的gridLayout中显示4个按钮,它们占用了50%的可用空间: 也许这会有所帮助

<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />



</GridLayout>

对于API 21之前的版本,请使用支持库:

你的依赖关系

然后在xml文件中:

<android.support.v7.widget.GridLayout
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:columnCount="2"
                    app:orientation="horizontal"
                    app:rowCount="1">

                    <TextView
                        android:text="1"
                        android:textStyle="bold"
                        app:layout_columnWeight="1"
                        />

                    <TextView
                        android:text="2"
                        android:textStyle="bold"
                        app:layout_columnWeight="1" />

</android.support.v7.widget.GridLayout>

当您使用
GridLayoutManager
时,您可以使用
SetPansizeLookup
。以下是我的项目中的一个片段,它应该有助于正确使用此方法:

if (mAdapter == null) {
    final int columnCount = getResources().getInteger(R.integer.numberGridColumns);
    mLayoutManager = new GridLayoutManager(getActivity(), columnCount);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (mAdapter.getItemViewType(position)) {
                case ListAdapter.VIEW_TYPE_ONE_COLUMN:
                    return columnCount;
                case RecipeListAdapter.VIEW_TYPE_FULL_COLUMN:
                default:
                    return 1;
            }
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecipeListAdapter(mPresenter);
    mRecyclerView.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged();

在占用50%可用空间的两列网格布局中动态添加视图:

GridLayout gridLayout = new GridLayout();

View view; //it can be any view

GridLayout.LayoutParams param = new GridLayout.LayoutParams();

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f);

param.width = 0;

view.setLayoutParams(param);

gridLayout.add(view);
以静态方式(在.xml文件中)



您是否尝试填充特定的高度/宽度?是否希望所有网格项都具有相同的高度?是的。我有两个专栏。我希望它们的宽度是屏幕宽度的50%,所以它们加在一起等于屏幕的宽度。然后它们的高度与宽度相同(以像素为单位)。所以它们是正方形的。你找到解决办法了吗?如果是,请分享。我也想要同样的功能。我想让我的按钮尽可能长,我使用了你提供的代码。它可以工作,但我想知道为什么它在
活动中的
onCreate
中不起作用。有什么想法吗?我现在提供了一个API 21之前的支持库最新版本。只需添加依赖项-com.android.support:gridlayout-v7:22.0。0@oznus我试过了,还是没用。它不具有权重属性。当使用新的
GridLayout
时,您应该使用
app
命名空间,因此它将是
app:layout\u gravity=“fill”
根据我的经验,要使重量正常工作,您必须将
layou width
设置为
0dp
@NigamPatro希望这有助于提高警惕,因为我花了很长时间才弄清楚。如果您正在开发API 28或更高版本,则有一个名为AndroidX的新支持库可用。如果在gradle.properties文件中将
android.useAndroidX
设置为true,则旧的支持库将无法工作。出于某种原因,我就是这样。但要解决这个问题,您只需使用
androidx.gridlayout.widget.gridlayout
而不是
android.support.v7.widget.gridlayout
,您就可以开始了。它的工作非常有魅力!还有一个要与layout_height=“0dp”组合的layout_线宽。
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
<android.support.v7.widget.GridLayout
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:columnCount="2"
                    app:orientation="horizontal"
                    app:rowCount="1">

                    <TextView
                        android:text="1"
                        android:textStyle="bold"
                        app:layout_columnWeight="1"
                        />

                    <TextView
                        android:text="2"
                        android:textStyle="bold"
                        app:layout_columnWeight="1" />

</android.support.v7.widget.GridLayout>
xmlns:app="http://schemas.android.com/apk/res-auto"
if (mAdapter == null) {
    final int columnCount = getResources().getInteger(R.integer.numberGridColumns);
    mLayoutManager = new GridLayoutManager(getActivity(), columnCount);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (mAdapter.getItemViewType(position)) {
                case ListAdapter.VIEW_TYPE_ONE_COLUMN:
                    return columnCount;
                case RecipeListAdapter.VIEW_TYPE_FULL_COLUMN:
                default:
                    return 1;
            }
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecipeListAdapter(mPresenter);
    mRecyclerView.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged();
GridLayout gridLayout = new GridLayout();

View view; //it can be any view

GridLayout.LayoutParams param = new GridLayout.LayoutParams();

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f);

param.width = 0;

view.setLayoutParams(param);

gridLayout.add(view);
<android.support.v7.widget.GridLayout
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:alignmentMode="alignBounds"
   app:columnCount="2"
   app:columnOrderPreserved="false"
   app:orientation="horizontal"
   android:layout_margin="20dp"
   android:layout_marginBottom="30dp"
   android:padding="4dp"
   app:rowCount="2">

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_username"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/user_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_first_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/first_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_middle_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/middle_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_last_name" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/last_name" />

</android.support.v7.widget.GridLayout>