拉伸 GridView 以适应全屏
Stretch GridView to fit full screen
我想拉伸我的 GridView
,它在每个网格内包含一个 ImageView
和一个 TextView
,以覆盖 phone 的整个屏幕。
我已经尝试过很多网上给出的解决方案,但 none 对我有用。我已经尝试过的一些答案是:
How can I force a GridView to use the whole screen (regardless of display size)?
android How to stretch rows in the gridview to fill screen?
这是我的代码:
activity_layout.xml
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gaming">
<GridView
android:id="@+id/categoryGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:background="@android:color/transparent"
android:numColumns="2"
android:stretchMode="columnWidth"
android:layout_weight="1" />
</FrameLayout>
category_grid_single.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/gridImageView"
android:layout_width="60dip"
android:layout_height="60dip"
android:scaleType="centerCrop"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_margin="1dp"/>
<TextView
android:id="@+id/gridImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/gridImageView"
android:layout_margin="5dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:textColor="@color/colorWhite" />
</RelativeLayout>
CustomAdapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.main_category_grid_single, null);
grid.setMinimumHeight(mheight/3);//mheight is the Height of the screen
} else {
grid = convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.gridImageViewText);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridImageView);
textView.setText(category[position]);
imageView.setImageResource(ImageId[position]);
return grid;
}
我正在尝试实现类似于下图的 GridView
。但目前在设置我的 GridView
后,我在底部变得空 space 。如果有比 GridView
更好的方法来实现它,那么也请提出建议。
这就是我得到的
任何帮助将不胜感激。提前致谢!
您可以通过 GridLayout
, instead of a GridView
, by using the layout_columnWeight
and layout_rowWeight
attributes. GridLayout
was added in API level 14, but these attributes were not added until API level 21. But you can make use of the GridLayout
as well as the layout_columnWeight
and layout_rowWeight
attributes back to API level 7 by using the v7 GridLayout Support Library:
compile 'com.android.support:gridlayout-v7:25.0.1'
然后,您所要做的就是确保 GridLayout
中的每个单元格具有相等的 layout_columnWeight
和 layout_rowWeight
:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="YouTube"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Videos"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Apps"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Movies"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Games"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Shop"/>
</FrameLayout>
</android.support.v7.widget.GridLayout>
</android.support.design.widget.CoordinatorLayout>
这是我得到的结果:
重述中描述的网格要点的清单:
添加编译指令
- 当前使用:
compile 'com.android.support:gridlayout-v7:25.3.1'
将布局更新为 android.support.v7.widget.GridLayout
- 注意这里,我一开始错过了这个,犯了一个错误,省略了完整的包名
设置app:columnCount="2"
设置每个网格元素:
app:layout_columnWeight="1"
app:layout_rowWeight="1"
演示 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="2"
app:orientation="horizontal"
>
<TextView
android:id="@+id/m1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m1"/>
<TextView
android:id="@+id/m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m2"/>
<TextView
android:id="@+id/m3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m3"/>
<TextView
android:id="@+id/m4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m4"/>
</android.support.v7.widget.GridLayout>
我想拉伸我的 GridView
,它在每个网格内包含一个 ImageView
和一个 TextView
,以覆盖 phone 的整个屏幕。
我已经尝试过很多网上给出的解决方案,但 none 对我有用。我已经尝试过的一些答案是:
How can I force a GridView to use the whole screen (regardless of display size)?
android How to stretch rows in the gridview to fill screen?
这是我的代码:
activity_layout.xml
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gaming">
<GridView
android:id="@+id/categoryGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:background="@android:color/transparent"
android:numColumns="2"
android:stretchMode="columnWidth"
android:layout_weight="1" />
</FrameLayout>
category_grid_single.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/gridImageView"
android:layout_width="60dip"
android:layout_height="60dip"
android:scaleType="centerCrop"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_margin="1dp"/>
<TextView
android:id="@+id/gridImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/gridImageView"
android:layout_margin="5dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:textColor="@color/colorWhite" />
</RelativeLayout>
CustomAdapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.main_category_grid_single, null);
grid.setMinimumHeight(mheight/3);//mheight is the Height of the screen
} else {
grid = convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.gridImageViewText);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridImageView);
textView.setText(category[position]);
imageView.setImageResource(ImageId[position]);
return grid;
}
我正在尝试实现类似于下图的 GridView
。但目前在设置我的 GridView
后,我在底部变得空 space 。如果有比 GridView
更好的方法来实现它,那么也请提出建议。
这就是我得到的
任何帮助将不胜感激。提前致谢!
您可以通过 GridLayout
, instead of a GridView
, by using the layout_columnWeight
and layout_rowWeight
attributes. GridLayout
was added in API level 14, but these attributes were not added until API level 21. But you can make use of the GridLayout
as well as the layout_columnWeight
and layout_rowWeight
attributes back to API level 7 by using the v7 GridLayout Support Library:
compile 'com.android.support:gridlayout-v7:25.0.1'
然后,您所要做的就是确保 GridLayout
中的每个单元格具有相等的 layout_columnWeight
和 layout_rowWeight
:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="YouTube"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Videos"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Apps"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Movies"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:background="@color/black_alpha_26">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="VR Games"/>
</FrameLayout>
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Shop"/>
</FrameLayout>
</android.support.v7.widget.GridLayout>
</android.support.design.widget.CoordinatorLayout>
这是我得到的结果:
重述
添加编译指令
- 当前使用:
compile 'com.android.support:gridlayout-v7:25.3.1'
- 当前使用:
将布局更新为
android.support.v7.widget.GridLayout
- 注意这里,我一开始错过了这个,犯了一个错误,省略了完整的包名
设置
app:columnCount="2"
设置每个网格元素:
app:layout_columnWeight="1" app:layout_rowWeight="1"
演示 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="2"
app:orientation="horizontal"
>
<TextView
android:id="@+id/m1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m1"/>
<TextView
android:id="@+id/m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m2"/>
<TextView
android:id="@+id/m3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m3"/>
<TextView
android:id="@+id/m4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="m4"/>
</android.support.v7.widget.GridLayout>