作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想把图片放在按钮的文字上。我想将文本和图像之间的间距设置为零。
此外,还有一个尺寸为 200 dp x 200 dp 的按钮我希望将图像和文本布置在相同的垂直填充中。请帮助我。
Button b = findViewById(R.id.button);
Drawable d = getResources().getDrawable(R.drawable.ic_launcher_background,getTheme());
b.setCompoundDrawablesWithIntrinsicBounds(null,d,null,null);
b.setCompoundDrawablePadding(0);
b.setText("good");
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hellow", Toast.LENGTH_SHORT).show();
}
});
最佳答案
有另一种方法可以做到这一点,而且事实证明它更容易。您是否考虑过使用 TextView
并向其添加可绘制对象,而不是使用 Button
并尝试对其使用可绘制对象?它可以在您的 XML 中完成,您可以添加填充。类似的东西,
<TextView
android:id="@+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textColor="@color/text_secondary_dark"
android:drawablePadding="8dp"
android:drawableTop="@drawable/my_icon"
tools:drawableTint="#000"/>
如您所见,您可以添加可绘制对象、它的填充、它的色调等。
关于android - setCompoundDrawablePadding() 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53485117/
我想把图片放在按钮的文字上。我想将文本和图像之间的间距设置为零。 此外,还有一个尺寸为 200 dp x 200 dp 的按钮我希望将图像和文本布置在相同的垂直填充中。请帮助我。 Button
我的布局中有一个 TextInputEditText,我需要向其添加一个可绘制端。可绘制端出现,但我无法为其添加可绘制填充端。 下面是我试过的一段代码: editText.setCompoundD
我是一名优秀的程序员,十分优秀!