Gridview 不适用于 Android 中的所有屏幕 [英] Gridview not fit for all screens in Android

查看:27
本文介绍了Gridview 不适用于 Android 中的所有屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android 应用.在这方面,我遇到了一个问题,即我的 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:padding="5dp"><网格视图android:id="@+id/mGridView"android:layout_width="match_parent"android:layout_height="match_parent"android:columnWidth="100dp"机器人:重力=中心"机器人:水平间距=10dp"机器人:numColumns =3"android:stretchMode="spacingWidthUniform"android:verticalSpacing="10dp"/></LinearLayout>

gridview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"机器人:方向=垂直"机器人:填充=5dp"android:layout_width="match_parent"android:layout_height="match_parent"><图像视图android:id="@+id/mImageView"android:layout_width="35dp"android:layout_height="35dp"/><文本视图android:id="@+id/mTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"机器人:文本对齐=中心"android:textSize="10dp"/></LinearLayout>

谁能帮我解决这个问题?

解决方案

使用GridLayout得到想要的结果.首先添加 gridLayout <强>依赖:

编译'com.android.support:gridlayout-v7:27.0.1'

然后,添加以下xml代码:

 

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 .

Here I have the gridview inside the fragment

The 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>

And gridview.xml is

<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?

解决方案

Use GridLayout to acquire the desired result.Firstly add gridLayout dependancy:

compile 'com.android.support:gridlayout-v7:27.0.1'

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>

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:

这篇关于Gridview 不适用于 Android 中的所有屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆