Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android按钮,带可拉伸填充_Java_Android_Button_Drawable_Padding - Fatal编程技术网

Java Android按钮,带可拉伸填充

Java Android按钮,带可拉伸填充,java,android,button,drawable,padding,Java,Android,Button,Drawable,Padding,我想向左侧的按钮添加一个可绘制图像,并尝试了以下操作: 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.setCompoundDrawablesWithIntrins

我想向左侧的按钮添加一个可绘制图像,并尝试了以下操作:

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);  
但是我想在按钮的可拉延部分和边框之间的左边有一个填充物。不幸的是,没有
有什么帮助吗?

使用此API调用:

public void setCompoundDrawablePadding (int pad)

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


您必须通过更改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);


你不能将左边的填充设置为一个更大的值吗?我尝试了15-但没有区别好吧,好吧,这似乎是不可能的。我尝试的一切都失败了。我想我只需要在图像文件中添加一个透明的填充。但我愿意接受任何进一步的想法:)为了其他阅读本文的人,如果您设置“padding”属性,它将执行您想要的操作。但是paddingTop、paddingRight等属性不能正确覆盖父级,也没有相同的效果。因此,这种方法在某些情况下可能适用于某些人,但不适用于我。但我认为这会在绘图和文本之间添加填充。这不是我想要的,我尽力了,这个解决方案对我不起作用。你确定它有效吗?好吧,这是唯一不涉及代码的答案。虽然OP没有明确说明这是他想要的,但这正是我想要的。Android需要解决这个问题:(
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);
  <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" />