Android 拉伸GridView以适应全屏显示
我想拉伸我的Android 拉伸GridView以适应全屏显示,android,android-gridview,Android,Android Gridview,我想拉伸我的GridView,它在每个网格内包含一个ImageView和一个TextView,以覆盖手机的整个屏幕 我已经在网上尝试了很多解决方案,但没有一个对我有效。我已经尝试过的一些答案是: 这是我的密码: 活动_layout.xml <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_heigh
GridView
,它在每个网格内包含一个ImageView
和一个TextView
,以覆盖手机的整个屏幕
我已经在网上尝试了很多解决方案,但没有一个对我有效。我已经尝试过的一些答案是:
这是我的密码:
活动_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>
<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>
我正在尝试实现一个类似下图的GridView
。但是目前在设置了我的GridView
之后,我在底部得到了一个空的空间。如果除了GridView
之外,还有其他更好的方法来实现它,那么也请提出建议
这就是我得到的
任何帮助都将不胜感激。提前谢谢 您可以通过使用和属性,而不是使用
GridView
来实现这一点GridLayout
是在API级别14中添加的,但这些属性直到API级别21才添加。但是,您可以使用以下命令将GridLayout
以及layout\u columnWeight
和layout\u rowWeight
属性返回到API级别7:
然后,您所要做的就是确保GridLayout
中的每个单元格都具有相等的layout\u columnWeight
和layout\u 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>
您可以使用和属性,而不是网格视图来实现这一点GridLayout
是在API级别14中添加的,但这些属性直到API级别21才添加。但是,您可以使用以下命令将GridLayout
以及layout\u columnWeight
和layout\u rowWeight
属性返回到API级别7:
然后,您所要做的就是确保GridLayout
中的每个单元格都具有相等的layout\u columnWeight
和layout\u 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指令
- 当前使用:
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>
检查表,概述以下表格中所述网格的要点:
添加compile指令
- 当前使用:
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>
对于文本视图,执行android:layout\u width=“match\u parent”如果不起作用,则将权重设为1和see@Redman,我没有工作。同样的结果。你能保留你如何得到它的截图吗?你试过这个吗@朱尔斯,我已经试过了。不起作用。对于文本视图,请执行android:layout\u width=“match\u parent”如果不起作用,请将权重设为1和see@Redman,我没有工作。同样的结果。你能保留你如何得到它的截图吗?你试过这个吗@朱尔斯,我已经试过了。不起作用。