【问题标题】:How to add two Buttons next to each other? (Buttons with background image, not ImageButtons)如何添加两个相邻的按钮? (带有背景图像的按钮,而不是 ImageButtons)
【发布时间】:2012-01-03 19:25:57
【问题描述】:

我想要两个相邻的按钮,如下所示: [ 使用 ] [ 取消 ] (这些只是带有背景图像的按钮,而不是 ImageButtons)

但结果很奇怪,第一个按钮填满了所有空间,在线性布局中是这样的: [.............使用............] 并且取消按钮未显示。 两个按钮的 layout_width 都是“wrap_content”,而线性布局的方向是水平的,有什么问题?

得到一个代码:

 <LinearLayout 
   android:layout_width="fill_parent"
   android:orientation="horizontal"
   android:id="@+id/linearLayout2" 
   android:layout_gravity="bottom" 
   android:layout_height="fill_parent" 
   android:layout_weight="1">


       <Button 
       android:text="Use" 
       android:height="14dp"
       android:textSize="15sp"
       android:textColor="#ffffff"
       android:background="@drawable/button1"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/UseButtonDialog" 
       android:layout_gravity="bottom">
       </Button>


        <Button android:text="Cancel" 
        android:background="@drawable/button1"
        android:height="14dp"
        android:textSize="15sp"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/CancelButtonDialog" 
        android:layout_gravity="bottom">
        </Button>


   </LinearLayout>

我应该对图像做些什么?

【问题讨论】:

    标签: android android-button


    【解决方案1】:

    为两个按钮添加 layout_weight 属性。将其设置为 1。

    或者也许从线性布局中删除 layout_weight 也可以。

    【讨论】:

      【解决方案2】:

      使用这个:

      为每个组件提供weight=1(或为LinearLayout 提供weightsum=1,权重0.5 到Buttons

      <LinearLayout android:layout_width="fill_parent"
         android:orientation="horizontal"
         android:id="@+id/linearLayout2" 
         android:layout_gravity="bottom" 
         android:layout_height="fill_parent">
             <Button android:weight="1"
             android:text="Use" 
             android:height="14dp"
             android:textSize="15sp"
             android:textColor="#ffffff"
             android:background="@drawable/button1"
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:id="@+id/UseButtonDialog" 
             android:layout_gravity="bottom">
             </Button>
              <Button android:weight="1"
              android:text="Cancel" 
              android:background="@drawable/button1"
              android:height="14dp"
              android:textSize="15sp"
              android:textColor="#ffffff"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:id="@+id/CancelButtonDialog" 
              android:layout_gravity="bottom">
              </Button>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 2019-08-08
        • 1970-01-01
        相关资源
        最近更新 更多