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?
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" />
You have to add the padding to you drawable, by changing the 9 slices content area or by changing your drawable padding:
<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.
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);
使用此API调用:
public void setCompoundDrawablePadding (int pad)
<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" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.