【问题标题】:set three button in a line (Horizontally) android studio with LinearLayout使用LinearLayout在一行(水平)android studio中设置三个按钮
【发布时间】:2020-02-17 08:24:36
【问题描述】:

我正在开发一个用于学习的 android 应用程序。 并面临一些问题。 无法设置两个按钮中心。 我已经用这个设置了两个按钮 现在我需要在这两个中间的另一个按钮 并且视图将像this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:gravity="start">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad" />
    </LinearLayout>

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

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad" />
    </LinearLayout>

我需要这两者的中心。

【问题讨论】:

    标签: android xml button android-linearlayout


    【解决方案1】:

    使用 layout_weight

    <?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:layout_weight="3"
        android:orientation="horizontal">
    
        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#dde5ad"
            android:text="Set" />
    
        <Button
            android:id="@+id/dialog_neutral_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:layout_weight="1"
            android:text="your text" />
    
        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:layout_weight="1"
            android:text="No" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您需要为此使用layout_weightlayout_gravity

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="start"
              android:layout_weight="1"
              android:gravity="start"
              android:orientation="vertical">
      
              <Button
                  android:id="@+id/dialog_positive_btn"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#dde5ad"
                  android:text="Set" />
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_weight="1"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:gravity="center"
              android:orientation="vertical">
      
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#dde5ad"
                  android:text="Set" />
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:layout_gravity="end"
              android:gravity="end"
              android:orientation="vertical">
      
              <Button
                  android:id="@+id/dialog_negative_btn"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#dde5ad"
                  android:text="No" />
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

      • @TamalDev 很高兴这对你有帮助,你能投票给这个答案吗?所以其他人也会觉得这很有帮助。
      【解决方案3】:

      您可以摆脱不必要的线性布局。如果你只需要连续添加三个按钮,你可以只在水平的LinearLayout 上这样做:

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
          <Button
              android:id="@+id/dialog_positive_btn"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Set"
              android:background="#dde5ad"
              />
      
          <!-- this is your new Button -->
           <Button
              android:id="@+id/new_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Set"
              android:background="#dde5ad"
              />
      
          <Button
              android:id="@+id/dialog_negative_btn"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="No"
              android:background="#dde5ad"
              />
      </LinearLayout>
      

      另请注意,嵌套线性布局可能会影响您的性能。如果你需要做复杂的布局,试试constraint layout

      希望对您有所帮助,祝您编码愉快!

      【讨论】:

        【解决方案4】:

        您应该尝试为每个按钮设置 android:layout_weight="1",每个按钮占用可用宽度的 1/3,以便它们在水平方向上均匀分布。请参阅下面的布局,您有 2 个选项可以获得类似的结果,一个使用 gravity 而另一个使用 layout_weight。最终结果类似,但 layout_weight="1" 的选项更可取:

        <LinearLayout
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
        
                <Button
                    android:id="@+id/dialog_positive_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Set"
                    android:background="#dde5ad"
                    />
        
                <Button
                    android:layout_marginStart="20dp"
                    android:id="@+id/center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Center"
                    android:background="#dde5ad"
                    />
        
        
                <Button
                    android:layout_marginStart="20dp"
                    android:id="@+id/dialog_negative_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No"
                    android:background="#dde5ad"
                    />
            </LinearLayout>
        

        或者你有第二个选择:

        <LinearLayout
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"        
                android:orientation="horizontal">
        
                <Button
                    android:id="@+id/dialog_positive_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Set"
                    android:background="#dde5ad"
                    />
        
                <Button
                    android:layout_weight="1"
                    android:layout_marginStart="20dp"
                    android:id="@+id/center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Center"
                    android:background="#dde5ad"
                    />
        
        
                <Button
                    android:layout_weight="1"
                    android:layout_marginStart="20dp"
                    android:id="@+id/dialog_negative_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No"
                    android:background="#dde5ad"
                    />
            </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          使用这个 -

          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:layout_gravity="start"
                  android:gravity="start">
          
                  <Button
                      android:id="@+id/dialog_positive_btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Set"
                      android:background="#dde5ad"
                      />
              </LinearLayout>
           <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:layout_gravity="center"
                  android:gravity="center">
          
                  <Button
                      android:id="@+id/dialog_negative_btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:text="No"
                      android:background="#dde5ad"
                      />
              </LinearLayout>
              <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:layout_gravity="end"
                  android:gravity="end">
          
                  <Button
                      android:id="@+id/dialog_negative_btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="No"
                      android:background="#dde5ad"
                      />
              </LinearLayout>
              </LinearLayout>
          

          【讨论】:

            【解决方案6】:

            在使用LinearLayout时有一个LayoutWeights的概念,因为它是一个广泛的话题我建议你阅读官方文档LinearLayout

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="#dde5ad"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
            
            <Button
                android:id="@+id/dialog_positive_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#dde5ad"
                android:text="Set" />
            
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginTop="14dp"
                android:layout_marginBottom="14dp"
                android:background="#000" />
            
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#dde5ad"
                android:text="No" />
            
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginTop="14dp"
                android:layout_marginBottom="14dp"
                android:background="#000" />
            
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#dde5ad"
                android:text="No" />
            

            【讨论】:

            • 我想他加了xmlns:android="http://schemas.android.com/apk/res/android"
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-06
            • 1970-01-01
            相关资源
            最近更新 更多