使用线性布局android在一个视图中的4个图像

时间:2017-06-07 17:38:22

标签: android-layout

我希望使用LinearLayout同等重量制作2 x 2图像。有人可以帮助我谢谢。感谢您的帮助

我想让我的Android应用程序运行的xml,但是我遇到了一些问题。我试图在线性布局中并排显示相同大小的2个图像。它们之间必须有一个边距,左右两侧,顶部。

layout

我的代码只能给我3张图片,但我需要4张图片。 我很感激任何帮助解决这个问题

这是我的代码:

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="horiztontal"
    android:weightSum="2">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"

        />


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:weightSum="2">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageView3"
                android:layout_weight="1" />


        </LinearLayout>
    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

使用以下布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="2">
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">
        <ImageView
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <ImageView
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

注意:您可以使用Relative或ConstrainLayout进一步优化此布局。

答案 1 :(得分:0)

Use the Code split 4 images equally



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear1"
    android:orientation="horizontal"
    android:layout_weight="1">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_margin="5dp"
    android:id="@+id/imageview"/>
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_margin="5dp"
    android:id="@+id/imageview1"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear2"
    android:orientation="horizontal"
    android:layout_weight="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="5dp"
        android:id="@+id/imageview3"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="5dp"
        android:id="@+id/imageview14"/>
</LinearLayout>

每个图像视图设置Scaletype属性以设置为FitXY