如何将两个按钮并排放置在按钮底部的图像上?
<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>
答案 0 :(得分:1)
而不是使用按钮你可以使用线性布局(Clickable)并使用它的id代替Button id.something像这样......使用它你可以根据你的要求自定义你的按钮。
<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>
答案 1 :(得分:1)
只需将以下行添加到您的按钮并指定您要使用的可绘制内容。
android:drawableBottom="@drawable/image"
希望这有帮助。
修改强> 如果您想要使用您的drawables,即无需修改其尺寸,则上述方法有效。如果您希望修改可绘制的维度,那么您可以通过下面编写的简单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;
}
答案 2 :(得分: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>