【问题标题】:GridLayout overflows the screenGridLayout 溢出屏幕
【发布时间】:2018-08-11 11:54:23
【问题描述】:

我在网格布局中有图像。但是,网格布局行会溢出屏幕。我认为图像宽度不是问题的原因。如果我删除图像,网格布局线仍然溢出。

我该如何解决这个问题?

代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myGridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    tools:context="com.example.orhan9.mdtransitionanimation.MainActivity">

    <ImageView
        android:id="@+id/papagan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        app:srcCompat="@drawable/papagan"
        />
    <ImageView
        android:id="@+id/goril"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        app:srcCompat="@drawable/goril" />

    <ImageView
        android:id="@+id/okuz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        app:srcCompat="@drawable/okuz" />

    <ImageView
        android:id="@+id/ordek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        app:srcCompat="@drawable/ordek" />

    <Button
        android:id="@+id/btnTransition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="transition animation" />

</GridLayout>

布局屏幕:

【问题讨论】:

标签: android android-layout grid-layout android-gridlayout


【解决方案1】:

我可能会找到几种方法来帮助您:

  1. 尝试使用 Linearlayout 来扭曲您的图像,设置它的 widthheight 并在其中放置您的图像,如建议的 here(他们的解决方案是使用 TextView,但您也可以使用 Imageview)。
  2. 将图像尺寸设置为特定尺寸(以 dp 为单位)。
  3. 尝试使用android:layout_rowWeightandroid:layout_columnWeight,如here

这里有一个快速教程,可以帮助您更好地理解网格布局Grid Layout by Example

【讨论】:

    【解决方案2】:
    You can do this to avoid overflows the screen because in your case you have to declare height and width
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myGridLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="2"
            android:rowCount="2">
    <LinearLayout
        android:orientation="vertical">
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
            <ImageView
                android:id="@+id/a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_weight="1"
                app:srcCompat="@drawable/ffff"
                android:scaleType="fitXY"/>
            <ImageView
                android:id="@+id/b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="1"
                android:layout_row="0"
                app:srcCompat="@drawable/ffff"
                android:scaleType="fitXY"/>
        </LinearLayout>
        <LinearLayout
    
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
            <ImageView
                android:id="@+id/c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="1"
                android:layout_weight="1"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ffff"/>
    
    
            <ImageView
                android:id="@+id/d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="1"
                android:layout_weight="1"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ffff"/>
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2014-08-16
      • 1970-01-01
      • 2021-10-23
      • 2017-10-23
      • 2020-12-19
      相关资源
      最近更新 更多