4

如何将两个按钮并排放置,并在按钮底部放置一个图像?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<Button
    android:id="@+id/a_button"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:text="Button-a" />
<Button
    android:id="@+id/b_button"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:text="Button-b" />

 </LinearLayout>
4

3 回答 3

1

除了使用按钮,您可以使用线性布局(可点击)并使用它的 id 而不是 Button id。类似这样的东西......使用它,您可以根据您的要求自定义您的 Button。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

//按钮-A

    <LinearLayout
        android:id="@+id/a_button"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.50"
        android:orientation="vertical" 
    android:clickable="true">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:text="Text-A"
            android:paddingTop="10dip"
             />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/image_A"
            android:paddingTop="13dip"  />

    </LinearLayout>

//对于水平分割线

     <TextView
        android:layout_width="2dp"
        android:layout_height="fill_parent"
        android:background="#303030"
        android:paddingTop="20dip" />

//按钮-B

     <LinearLayout

         android:id="@+id/b_button"
         android:layout_width="0dp"
         android:layout_height="fill_parent"
         android:layout_weight="0.50"
         android:orientation="vertical" 
     android:clickable="true">


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text-B"
            android:gravity="center_horizontal|center_vertical"
            android:paddingTop="10dip" />

        <ImageView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:scaleType="fitXY"
             android:src="@drawable/image_B"
             android:paddingTop="13dip"
              />

    </LinearLayout>
</LinearLayout>
于 2013-05-15T08:38:57.493 回答
1

只需将下面的行添加到您的两个按钮并指定您要使用的可绘制对象。

android:drawableBottom="@drawable/image"

希望这可以帮助。

编辑: 如果您想按原样使用您的可绘制对象,即不修改其尺寸,则上述方法有效。如果您还希望修改可绘制尺寸,那么您可以通过下面编写的简单 Java 代码来实现:

((Button)findViewById(R.id.button)).setCompoundDrawables(null, null, null, getDrawable(getApplicationContext(), R.drawable.image, 25));

…………

Drawable getDrawable(Context context, int drawable, float form_factor) {
    Drawable imgDrawable = context.getResources().getDrawable(drawable);
    imgDrawable.setBounds(0, 0, (int)form_factor, (int)form_factor);
    return imgDrawable;
}
于 2013-05-15T08:43:44.077 回答
1

您可以使用 Drawable_Bottom 属性将图像放置在按钮的底部。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:attr/buttonBarStyle"
android:orientation="horizontal" >

<Button
    android:id="@+id/a_button"
    style="@android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableBottom="@android:drawable/btn_star"
    android:text="Button-a" />

<Button
    android:id="@+id/b_button"
    style="@android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableBottom="@android:drawable/btn_star"
    android:text="Button-b" />

</LinearLayout>
于 2013-05-15T08:44:30.297 回答