简体   繁体   中英

2 column 3 row Exact Fit, Relative Layout (Android)

I have six ImageView blocks I want to fit in a vertical layout, 2 columns by 3 rows.

How do I get these to fit exactly, so that I will have six evenly distributed blocks.

There is a slight overlap between the last four blocks.

(ImageView 3-6 are basically the same as the first two.)

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

Use LinearLayout. You can use android:scaleType="fitXY" in ImageView, if you want the image to be fill its parent. Please have a look at ImageView.ScaleType for your reference.

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

Try this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM