繁体   English   中英

带抽屉的Android按钮 - 填充

[英]Android Button with Drawable - Padding

我想在左侧的Button上添加Drawable Image并尝试以下操作:

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);  

但是我希望在可绘制和按钮边框之间的左边有一个填充。 不幸的是,没有。
有帮助吗?

这将生成文本+图像按钮(左图像和后图文字)

   <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" />

您必须通过更改9切片内容区域或更改可绘制填充来向您绘制添加填充:

<padding
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp" />

如果使用选择器,则必须将填充规则添加到每个可绘制状态。

使用此代码:

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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM