【问题标题】:Nested vertical layout in horizontal layout水平布局中的嵌套垂直布局
【发布时间】:2025-02-19 17:20:02
【问题描述】:

我正在创建一个 android 应用程序,我想创建一个布局,它有两个并排的按钮,下面有三个按钮。我正在尝试做的一个例子可以在这里找到:

http://mycolorscreen.com/2014/11/29/flatdrop-zw-skin/

我正在努力让两个按钮在水平线性布局中并排,并且在垂直线性布局中也让三个按钮。由于第一个线性布局是水平的,它会使我的按钮也变成水平的。我的代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="1">

<TextView
        android:layout_width="164dp"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_gravity="left"
        android:layout_weight="0.1" />

    <TextView
        android:layout_width="174dp"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_gravity="right"
        android:layout_weight="0.1" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    style="@style/AppTheme"
    android:longClickable="false"
    android:weightSum="1">



    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnFitnessGoalsDefaultString"
        android:textColor="#FFFFFF"
        android:id="@+id/btnFitnessGoals"
        android:background="@drawable/buttondefault"
        android:layout_marginBottom="10dp"
        android:layout_weight="0.10"
        android:layout_gravity="top" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="@string/btnNutritionGoalsDefaultString"
        android:textColor="#FFFFFF"
        android:id="@+id/btnNutritionGoals"
        android:background="@drawable/buttondefault"
        android:layout_weight="0.10" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnBonusEventsDefaultString"
        android:textColor="#FFFFFF"
        android:id="@+id/btnBonusEvents"
        android:background="@drawable/buttondefault"
        android:layout_weight="0.10" />
</LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android xml android-layout android-fragments android-studio


    【解决方案1】:

    你需要做这样的事情

    <LinearLayout
        android:orientation="vertical">
    
          <LinearLayout
             android:orientation="horizontal">
    
               <!-- horizontal buttons here -->
    
          </LinearLayout>
    
          <LinearLayout
            android:orientation="vertical">
    
               <!-- vertical buttons here -->
    
          </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 感谢您的帮助!!
    【解决方案2】:

    使用这样的东西:

    <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:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:text="Button" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:text="Button" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <Button
                    android:id="@+id/button3"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button4"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button5"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:text="Button" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      简单一点:

      <LinearLayout
        android:orientation="vertical">
      
        <LinearLayout
           android:orientation="horizontal">
      
             <!-- horizontal buttons here -->
      
        </LinearLayout>
      
             <!-- vertical buttons here -->
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        此布局适用于所有设备屏幕。试试吧。

        <?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" >
        
        <!-- 2 buttons side by side -->
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
        
            <!-- btn 1 -->
        
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ff00"
                android:foreground="?android:attr/selectableItemBackground" >
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Button 1" />
            </FrameLayout>
        
            <!-- btn 2 -->
        
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#446600"
                android:foreground="?android:attr/selectableItemBackground" >
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Button 2" />
            </FrameLayout>
        </LinearLayout>
        
        <!-- three buttons underneath -->
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical" >
        
            <!-- btn 3 -->
        
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#dd77ff"
                android:foreground="?android:attr/selectableItemBackground" >
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Button 3" />
            </FrameLayout>
            <!-- btn 4 -->
        
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#9977ff"
                android:foreground="?android:attr/selectableItemBackground" >
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Button 4" />
            </FrameLayout>
            <!-- btn 5 -->
        
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#1277ff"
                android:foreground="?android:attr/selectableItemBackground" >
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Button 5" />
            </FrameLayout>
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:
          <LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1">
          
              <TextView
                  android:id="@+id/costOperatingText"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Operating cost:" />
          
              <TextView
                  android:id="@+id/button2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="0"/>
          </LinearLayout>
          
          <LinearLayout
              android:id="@+id/linearLayout2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="6">
          
              <TextView
                  android:id="@+id/earnings"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Daily Earnings:  " />
          
              <TextView
                  android:id="@+id/earningsValue"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="0" />
          </LinearLayout>
          

          【讨论】: