【问题标题】:2 column 3 row Exact Fit, Relative Layout (Android)2 列 3 行精确匹配,相对布局 (Android)
【发布时间】:2017-01-06 02:28:38
【问题描述】:

我有六个 ImageView 块,我想在垂直布局中放入 2 列乘 3 行。

我如何让这些完全适合,这样我就有六个均匀分布的块。

最后四个块之间有轻微的重叠。

(ImageView 3-6与前两个基本相同。)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/imageView"
    android:padding="96dp"
    android:background="@color/colorAccent" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView"
    android:layout_alignParentRight="true"
    android:id="@+id/imageView2"
    android:background="@color/colorPrimary"
    android:padding="96dp" />

<ImageView
   3 />

<ImageView
   4  />

<ImageView
   5 />

<ImageView
   6  />

</RelativeLayout>

【问题讨论】:

    标签: android xml android-relativelayout


    【解决方案1】:

    使用线性布局。如果您希望图像填充其父级,可以在 ImageView 中使用android:scaleType="fitXY"。请查看ImageView.ScaleType 供您参考。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="horizontal">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个代码:

       <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:weightSum="2"
      android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:weightSum="3"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:weightSum="3"
          android:orientation="horizontal">
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:scaleType="fitXY"
              android:src="@mipmap/ic_launcher"/>
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:scaleType="fitXY"
              android:src="@mipmap/ic_launcher"/>
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:scaleType="fitXY"
              android:src="@mipmap/ic_launcher"/>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2017-11-24
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多