android - 在 LinearLayout 中嵌套 RelativeLayouts

标签 android android-layout android-linearlayout android-relativelayout

我正在尝试在 Android 中实现以下布局,主标题位于顶部,三个副标题在下方等距分布,下方有一些按钮:

enter image description here

我能够使用以下 XML 完成此操作,但我觉得我没有正确使用 RelativeLayout 和 LinearLayout:

<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:background="#cfff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Title"
        android:textSize="80sp"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight=".5">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/text_caption1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Subheading"
                android:gravity="center"
                android:textSize="18sp"/>

            <TextView
                android:id="@+id/text_number1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text_caption1"
                android:text="1"
                android:layout_weight="1"
                android:textSize="80sp"
                android:gravity="center"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/text_caption2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Subheading"
                android:gravity="center"
                android:textSize="18sp"/>

            <TextView
                android:id="@+id/text_number2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text_caption2"
                android:text="10"
                android:layout_weight="1"
                android:textSize="80sp"
                android:gravity="center"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/text_caption3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Subheading"
                android:gravity="center"
                android:textSize="18sp"/>

            <TextView
                android:id="@+id/text_sets"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text_caption3"
                android:text="3"
                android:layout_weight="1"
                android:textSize="80sp"
                android:gravity="center"/>

        </RelativeLayout>
    </LinearLayout>

    <!-- Button1 -->
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Button1"
        android:textSize="20sp"/>

    <!-- Button2 -->
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Button2"
        android:textSize="20sp" />

</LinearLayout>

我比较关注副标题部分,但欢迎对整体布局提出任何建议。因此,我使用了 3 个不同的 RelativeLayouts 来放置“副标题”标题及其各自的编号,并将它们嵌套在一个水平的 LinearLayout 中,使它们在水平方向上彼此相邻。

这似乎造成了一些问题,因为 Android Studio 提示我在嵌套布局中使用 layout_weight 不利于性能。然而,当我摆脱 layout_weight 时,一切都崩溃了,我只看到“标题”和一个副标题。我还想知道我是否可以仅使用一个 RelativeLayout 而不使用嵌套来更优雅地完成此操作,但我不知道如何在不使用 LinearLayout 的情况下编写这样的代码。

提前感谢您的任何建议!

最佳答案

是的,你可以用一个 RelativeLayout 并且没有嵌套来更优雅地完成这个 我希望这就是你要找的东西

 <RelativeLayout 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:background="#cfff"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Title"
        android:textSize="80sp" />

    <TextView
        android:id="@+id/text_caption1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/text_number1"
        android:text="Subheading"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/text_number1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="left"
        android:text="1"
        android:textSize="80sp" />


    <TextView
        android:id="@+id/text_caption2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/text_number2"
        android:gravity="center"
        android:text="Subheading"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/text_number2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="2"
        android:textSize="80sp" />

    <TextView
        android:id="@+id/text_caption3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/text_number3"
        android:gravity="right"
        android:text="Subheading"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/text_number3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="right"
        android:text="3"
        android:textSize="80sp" />


    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:text="Button1"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button2"
        android:textSize="20sp" />
    </RelativeLayout>

关于android - 在 LinearLayout 中嵌套 RelativeLayouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063758/

相关文章:

android - 如何在android中打开文件保存对话框?

具有灵活高度的ListView中的Android自定义行布局

android - 有没有办法跟踪这个异常 : focus search returned a view that wasn't able to take focus

Android WebView 缩放字体大小

android - 奥利奥本地化有时不起作用

android - Android 样式 xml 中的多个父项

android - 如何布局我的 android 应用程序?

android - GridView 滚动一行

java - WindowManager addView() 不尊重 MATCH_PARENT

android - 均匀缩放线性布局中的图像