【问题标题】:How to place one object beneath another with LinearLayout?如何使用 LinearLayout 将一个对象放在另一个对象下方?
【发布时间】:2019-01-14 20:35:17
【问题描述】:

我必须使用LinearLayout 在 Android Studio 中制作这个小应用类型的东西。

说明说我们必须使用vertical 方向作为根标签(顶部的文本显示“这是我的小部件”),然后每行使用horizontal 方向。我唯一的问题是如何将东西放在不同的行上?

它应该看起来像:

第 1 行:“这是我的小部件”(垂直方向)

第 2 行: BUTTON IMAGEBUTTON

第 3 行: CHECKBOX SWITCH

我的问题是,当我创建一个新的嵌套 LinearLayout 时,它会显示在第 2 行。如何使用horizontal 方向创建第 3 行?我尝试用vertical 方向放置一个空的LinearLayout 东西,以便将两行分开,但这不起作用。如何使CheckBox 和开关显示在按钮下方?我必须使用LinearLayout,并且方向必须是水平的。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_message"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">


<Button
    android:gravity="fill"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="200dp"
    android:text="@string/ButtonText" />

<ImageButton
    android:gravity="fill"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:layout_width="200dp"
    android:src="@drawable/flag"
    android:scaleType="centerInside"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Switch
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android xml android-studio-3.0


    【解决方案1】:

    这是您的布局的简要外观:

    <LinearLayout
        android:id="@+id/root_linear_layout"
        android:orientation="vertical"
        // other attributes... > 
    
        <TextView
            // This is ROW 1!
            //welcome text goes here />
    
        <LinearLayout
            // This is ROW 2!
            android:orientation="horizontal">
    
               <Button
                   ... />
    
               <ImageButton 
                    .../>
    
         </LinearLayout>   <-- Closing of the Row2 horizontal LinearLayout
    
    
        <LinearLayout
            // This is ROW 3!
            android:orientation="horizontal">
    
               <CheckBox
                    ... />
    
               <Switch
                     .../>
    
         </LinearLayout> <-- Closing of the Row3 horizontal LinearLayout
    </LinearLayout> <-- Closing of the root vertical LinearLayout
    

    您可以根据需要填写其他属性(id、高度、宽度等)。

    我注意到您对每个Views 都使用了android:layout_weight 属性。如果您使用此属性,最好将android:weightSum 用于Views 父布局。例如,如果您需要 ButtonImageButton 的宽度比为 50:50:

    <LinearLayout
       android:weightSum="2">  <--- starting of Row2 LinearLayout
    
          <Button
              android:layout_weight="1"     ... />
    
          <ImageButton 
              android:layout_weight="1      ... />
    
    </LinearLayout> <--- closing of Row2 LinearLayout
    

    有关weight 的更多详细信息,请参阅here :)

    【讨论】:

    • 有道理,没想到我必须关闭第一个。谢谢。
    • @J.Griff2 很高兴我的回答对您有所帮助。如果您能接受我的回答,那就太好了:) 快乐编码
    【解决方案2】:

    像这样:

    <?xml version="1.0" encoding="utf-8"?>
    

        <Button
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:text="Button one" />
    
        <ImageButton
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:scaleType="centerInside"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            android:text="Check Box"
            android:layout_weight="1"/>
    
        <Switch
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多