android xml:textview,下面有两个相邻的图像视图

zfycwa2u  于 2021-09-29  发布在  Java
关注(0)|答案(3)|浏览(278)

我想在android中创建如下布局:
这就是我想要的结果
到目前为止,我的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="13dp"
    android:text="TextView"
    android:textSize="24sp"
    android:textStyle="bold" />

    <ImageView
    android:id="@+id/imageView2"
    android:layout_width="216dp"
    android:layout_height="217dp"
    android:layout_below="@+id/textView"
    android:scaleType="fitStart"
    android:adjustViewBounds="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/ic_launcher_background" />
    </RelativeLayout>

如果有人能帮我,我将不胜感激

omtl5h9j

omtl5h9j1#

你可以用 LinearLayout 如果希望图像间隔均匀:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="TextView"
        android:textAlignment="center"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/default_image" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/default_image" />
    </LinearLayout>
</RelativeLayout>
ozxc1zmp

ozxc1zmp2#

我建议在这种情况下使用linearlayout,因为布局非常简单。试试看。在这里,图像的宽度被imageview1和imageview2中的属性android:layout_weight=“1”精确减半,结果权重为2(在linearlayout中设置android:weightsum=“2”,尝试更改它,您将看到它的工作原理)。
此外,在更复杂的布局中,我建议使用constraintlayout而不是relativelayout。
祝你好运

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="13dp"
            android:paddingHorizontal="20dp"
            android:paddingTop="20dp"
            android:paddingBottom="40dp"
            android:text="TextView"
            android:textColor="@android:color/white"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_launcher_background"/>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:adjustViewBounds="true"
            android:layout_marginleft="5dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/ic_launcher_background"/>
    </LinearLayout>
</LinearLayout>
u5i3ibmn

u5i3ibmn3#

使用约束布局,不必嵌套布局。只需定义每个视图的开始、结束、顶部和底部约束,即可获得所需的布局。例如,两个图像视图的顶部应约束到textview的底部,然后左imageview的起点应约束到父容器的起点,而右imageview的终点应约束到父容器的终点。然后将它们相互约束。i、 e右imageview的起点与左imageview的终点相对,反之亦然。

相关问题