java Android Button with Drawable - Padding
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16843163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Button with Drawable - Padding
提问by Tobias Baumeister
I want to add a Drawable Image to my Button on the left and tried the following:
我想在左侧的按钮中添加一个可绘制的图像并尝试以下操作:
button = new Button(this);
button.setWidth(screen_dimens(0.25, "w")); //25% of screen width
button.setPadding(5, 5, 0, 5);
Drawable img = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
But I want to have a padding on the left between the drawable and the border of the button. Unfortunately, there is none.
Any help?
但我想在可绘制对象和按钮边框之间的左侧有一个填充。不幸的是,没有。
有什么帮助吗?
回答by Ankit Arjaria
This will generate text + image button ( Image on Left & Text After That )
这将生成文本 + 图像按钮(左侧图像和之后的文本)
<Button
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:paddingLeft="20dp"
style="@style/whiteText"
android:onClick="openWhyAshoka"
android:drawableLeft="@drawable/arrow"
android:drawablePadding="50dp"
android:text="Ankit" />
回答by Ratata Tata
You have to add the padding to you drawable, by changing the 9 slices content area or by changing your drawable padding:
您必须通过更改 9 切片内容区域或更改可绘制填充来将填充添加到可绘制对象中:
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
If you are using selector you have to add the padding rule to each one of the drawable states.
如果您使用选择器,则必须将填充规则添加到每个可绘制状态。
回答by Bhaskar Kumar Singh
Use this Code:
使用此代码:
Drawable image = (Drawable)getResources().getDrawable(R.drawable.onstar_mute_unmute_button_selector);
ImageButton button=(ImageButton)findViewById(R.id.imageButton);
button.setPadding(0,10,5,0);
button.setGravity(Gravity.RIGHT);
button.setBackground(image);
回答by Null Pointer Exception
<Button
android:id="@+id/otherapps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/btn_background"
android:text="@string/other_apps"
android:layout_weight="1"
android:fontFamily="cursive"
android:layout_marginBottom="10dp"
android:drawableLeft="@drawable/ic_more"
android:paddingLeft="8dp" //for drawable padding to the left
android:textColor="@android:color/holo_red_light" />
回答by Steveo
Use this API call:
使用此 API 调用:
public void setCompoundDrawablePadding (int pad)