【问题标题】:How to put two buttons on same line in Android如何在Android中将两个按钮放在同一行
【发布时间】:2012-04-17 05:55:22
【问题描述】:

如何在我的 Android 应用程序的登录布局中将两个按钮放在同一行?

【问题讨论】:

  • 您需要提出更有意义的问题。尝试提供更多关于您到目前为止所做的事情的详细信息。

标签: android android-layout android-button


【解决方案1】:

只需做一个线性布局。将方向设置为水平并添加两个按钮。它准备好你会得到你想要的。在发布此类问题之前尝试谷歌搜索你会得到肯定的答案。这段代码会帮助你。

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

如果您想在布局中放置两个按钮。将两个按钮的权重设为 1。

【讨论】:

    【解决方案2】:

    最好的解决方案是在LinearLayout中放置2个按钮(宽度相等)

    还有一件事,如果你想要相等的“宽度”按钮,那么选择宽度为 0dp 且所有按钮权重相同的按钮。

    如果您想要相同的“高度”按钮,则使用高度为 0dp 且所有按钮权重相同的按钮。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>
    

    【讨论】:

    • 我真的推荐这个解决方案。在调整 LinearLayout 容器上的项目时,使用“layout_weight”是最佳选择。
    • 感谢伊万的评论。
    【解决方案3】:

    使用这个把两个按钮放在同一行....

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    
       >
        <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:layout_alignParentBottom="true"
        />
          <Button
        android:id="@+id/cancel"     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_toRightOf="@+id/login"
        android:layout_alignParentBottom="true"
        />
          </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      您需要添加线性布局(水平)。然后您可以在一行中添加多个按钮....
      您也可以为此使用相对布局。
      这是给你的代码......

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
      <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
          <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
          <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
          <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
          <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        您可以使用一种水平方向的线性布局并在其中添加两个按钮

        <LinearLayout
        
           <Button1.../>
           <Button2.../>
        </LinearLayout>
        

        【讨论】:

          【解决方案6】:

          我认为你需要使用RelativeLayout。你可以这样做:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content" android:orientation="horizontal"
              android:layout_width="fill_parent">
          
              <Button
          
                  android:text="@+id/Button01" 
                  android:id="@+id/Button01"
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true">
              </Button>
          
              <Button
          
                  android:text="@+id/Button02" 
                  android:id="@+id/Button02"
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true">
              </Button>
          
          </RelativeLayout>
          

          你也可以参考这个。 http://www.mkyong.com/android/android-relativelayout-example/

          希望这会对你有所帮助。

          【讨论】:

            【解决方案7】:

            如果您将按钮放置在 LinearLayout 中,请将 Orientation 值指定为“Vertical”,它会自动将按钮放置在同一行中。如果您使用的是 RelativeLayout,那么对于一个按钮,使用 android:layout_toLeftOf 或 android:layout_toRightOf 并将值作为其他按钮的 ID。如果你答对了,请将其标记为答案。谢谢...

            【讨论】:

              【解决方案8】:

              您可以使用水平方向的线性布局并在其中添加两个按钮:-

                <LinearLayout
                  android:id="@+id/btn_action_wallet_container"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="20dp"
                  android:layout_marginEnd="20dp"
                  android:layout_below="@id/ll_total_coin_container"
                  android:layout_marginTop="20dp"
                  android:orientation="horizontal">
              
              
              
                  <Button
                      style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:fontFamily="@font/lato_black"
                      android:layout_weight="2"
                      android:text="Redeem" />
              
                  <Space
                      android:layout_width="20dp"
                      android:layout_height="wrap_content" />
              
                  <com.google.android.material.button.MaterialButton
                      style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:text="Earn More"
                      android:layout_weight="2"
                      android:fontFamily="@font/lato_black" />
              
              
              </LinearLayout>
              

              运行它,看看它是如何工作的

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-03-16
                • 1970-01-01
                • 1970-01-01
                • 2023-02-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-06-16
                相关资源
                最近更新 更多