Android 相对布局与线性布局或两者的混合

Android 相对布局与线性布局或两者的混合,android,user-interface,layout,user-experience,Android,User Interface,Layout,User Experience,我一直在尝试根据我在纸上设计的东西来设计布局,但也没能让它按照我想要的方式对齐,同时也能适应所有屏幕。我用一个简单的结构来设计,但没有成功。我没能在以太布局中对齐所有内容。我尝试了相对和线性的混合,然后尝试了两者的融合 我将提供图像,以显示布局风格,我要去 提前谢谢 不久前我做了一个类似的设计。我使用线性布局(垂直)来制作不同的行,当一行中需要多个列时,我使用线性布局(水平),如您的案例中的图像按钮1和图像按钮2。注意不同视图中的层次结构,因为如果操作错误,它会变得非常混乱。报复性布局与线性布局

我一直在尝试根据我在纸上设计的东西来设计布局,但也没能让它按照我想要的方式对齐,同时也能适应所有屏幕。我用一个简单的结构来设计,但没有成功。我没能在以太布局中对齐所有内容。我尝试了相对和线性的混合,然后尝试了两者的融合

我将提供图像,以显示布局风格,我要去

提前谢谢


不久前我做了一个类似的设计。我使用线性布局(垂直)来制作不同的行,当一行中需要多个列时,我使用线性布局(水平),如您的案例中的图像按钮1和图像按钮2。注意不同视图中的层次结构,因为如果操作错误,它会变得非常混乱。

报复性布局与线性布局

我认为他们不应该这样,因为他们都是不同的,服务于不同的目的

线性布局

您应该使用线性布局,您必须在视图中进行批量的非布局更改,如添加、删除或创建动态,因为它不会重建整个布局,只需更改参数即可

相对布局

您应该在希望实现某种对齐的位置使用,并且不希望小部件再次呈现,因为相对布局的子级中的任何更改都会重新排列/重新绘制整个小部件


希望这能提供更好的图片

你应该试试这张

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      >

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
        >

        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            >

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </LinearLayout>
</LinearLayout>

在布局中使用您想要的按钮/文本以及您想要的颜色


希望这一条对您有所帮助。

您可以使用约束布局进行此设计,它还可以扁平化层次结构

线性布局相对布局都可以用于实现上述目标,但线性布局更快地完成任务

对于上面所附的图片,以下是开始使用线性布局所需的内容

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

    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ocean"/>

    <TextView
        android:id="@+id/title"
        android:text="Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:background="#ccddff"
        android:gravity="center"
        android:padding="20dp"/>

    <TextView
        android:id="@+id/block_of_text"
        android:text="Sed aliquam ultrices mauris. Sed cursus turpis vitae tortor. Phasellus consectetuer vestibulum elit. Quisque malesuada placerat nisl."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:background="#B09E99"
        android:gravity="left"
        android:padding="8dp"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:weightSum = "2">
       <ImageButton
            android:id="@+id/image_button1"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/ibtn1"/>
       <ImageButton
            android:id="@+id/image_button2"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/ibtn2"/>
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:weightSum = "2">
       <ImageButton
            android:id="@+id/image_button3"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/ibtn3"/>
       <ImageButton
            android:id="@+id/image_button4"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/ibtn4"/>
    </LinearLayout>

    <ImageButton
        android:id="@+id/image_button5"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:src="@drawable/ibtn5"/>

 </LinearLayout>

请尝试此选项
[1]: https://i.stack.imgur.com/MmiUc.png

更好的使用没有“报复性布局”之类的东西,我想你是说“相对布局”进行编辑。这看起来可以单独使用多个
线性布局来完成。但是,如果您想要一个单一的根
视图
,您可以将
表格布局
与多个
表格行
一起使用。图像的部分可以是一个
TableRow
,其
weightSum
为2,其中包含2个
ImageView
s,每个
layout\u weight
为1。
    Please Try This

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

    <LinearLayout android:layout_width="match_parent"
                  android:layout_weight="2.2"
                  android:background="#DFF7FA"
                  android:gravity="center"
                  android:layout_height="match_parent">

        <TextView android:layout_width="match_parent"
                  android:text="Image/Logo"
                  android:textAlignment="center"
                  android:textSize="25sp"
                  android:textColor="@android:color/black"
                  android:layout_height="wrap_content"/>



    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
                  android:layout_weight="2.6"
                  android:gravity="center"
                  android:background="#B9CEBC"
                  android:layout_height="match_parent">

        <TextView android:layout_width="match_parent"
                  android:text="Title"
                  android:textAlignment="center"
                  android:textSize="25sp"
                  android:textColor="@android:color/black"
                  android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_weight="2.3"
                  android:gravity="center"
                  android:background="#CECBCB"
                  android:layout_height="match_parent">

        <TextView android:layout_width="match_parent"
                  android:text="Block of Text"
                  android:textAlignment="center"
                  android:textSize="25sp"
                  android:textColor="@android:color/black"
                  android:layout_height="wrap_content"/>

    </LinearLayout>

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

        <LinearLayout android:layout_width="match_parent"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:background="#E0F5FE"
                      android:layout_height="match_parent">

            <TextView android:layout_width="match_parent"
                      android:text="ImageButton1"
                      android:textAlignment="center"
                      android:textSize="25sp"
                      android:textColor="@android:color/black"
                      android:layout_height="wrap_content"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
                      android:layout_weight="1"
                      android:background="#B2E4FC"
                      android:gravity="center"
                      android:layout_height="match_parent">

            <TextView android:layout_width="match_parent"
                      android:text="ImageButton2"
                      android:textAlignment="center"
                      android:textSize="25sp"
                      android:textColor="@android:color/black"
                      android:layout_height="wrap_content"/>

        </LinearLayout>


    </LinearLayout>

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

        <LinearLayout android:layout_width="match_parent"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:background="#B2E4FC"
                      android:layout_height="match_parent">

            <TextView android:layout_width="match_parent"
                      android:text="ImageButton3"
                      android:textAlignment="center"
                      android:textSize="25sp"
                      android:textColor="@android:color/black"
                      android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout android:layout_width="match_parent"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:background="#E0F5FE"
                      android:layout_height="match_parent">

            <TextView android:layout_width="match_parent"
                      android:text="ImageButton4"
                      android:textAlignment="center"
                      android:textSize="25sp"
                      android:textColor="@android:color/black"
                      android:layout_height="wrap_content"/>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_weight="2.4"
                  android:gravity="center"
                  android:background="#7FD7FF"
                  android:layout_height="match_parent">

        <TextView android:layout_width="match_parent"
                  android:text="ImageButton5"
                  android:textAlignment="center"
                  android:textSize="25sp"
                  android:textColor="@android:color/black"
                  android:layout_height="wrap_content"/>

    </LinearLayout>
</LinearLayout>



  [1]: https://i.stack.imgur.com/MmiUc.png