Gridview不适用于Android中的所有屏幕
问题描述:
我正在使用Android应用程序.在这个问题上,我的GridView
和图像无法适应所有屏幕尺寸.
I am working in an Android app. In this I have an issue that my GridView
and images are not get fit with all screen sizes .
这是片段中的gridview
fragment_reminders.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout_Gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp">
<GridView
android:id="@+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
gridview.xml
是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/mImageView"
android:layout_width="35dp"
android:layout_height="35dp" />
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="10dp" />
</LinearLayout>
有人可以帮助我解决此问题吗?
Can anyone help me to fix this issue?
答
使用GridLayout
获取所需的结果.首先添加 gridLayout
依赖性:>
Use GridLayout
to acquire the desired result.Firstly add gridLayout
dependancy:
compile 'com.android.support:gridlayout-v7:27.0.1'
然后,添加以下xml代码:
Then, add the following xml code:
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="3">
<FrameLayout
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_lock_idle_alarm"
android:layout_marginBottom="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Alarms"/>
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.GridLayout>
在上面的代码中,我刚刚添加了1个元素.只需复制&粘贴此FrameLayout
并更改文本&图像相应.添加12个元素后,输出应如下所示:
In the above code, I've just added 1 element. Just copy & paste this FrameLayout
and change texts & images accordingly. After adding 12 elements, the output should look as below: