Android:以编程方式指定按钮底部的图像
我试图通过编程方式将图像设置为底部的按钮。我用java代码创建了show more按钮,希望在按钮底部指定箭头图像,但没有找到以编程方式指定图像的方法。Android:以编程方式指定按钮底部的图像,android,image,button,Android,Image,Button,我试图通过编程方式将图像设置为底部的按钮。我用java代码创建了show more按钮,希望在按钮底部指定箭头图像,但没有找到以编程方式指定图像的方法。 这是我的按钮代码 btnAddHotel = new Button(this); btnAddHotel.setText("show more"); btnAddHotel.setBackgroundColor(Color.TRANSPARENT); btnAddHotel.setTextColor(Color.
这是我的按钮代码
btnAddHotel = new Button(this);
btnAddHotel.setText("show more");
btnAddHotel.setBackgroundColor(Color.TRANSPARENT);
btnAddHotel.setTextColor(Color.WHITE);
btnAddHotel.setTypeface(faceNormal);
btnAddHotel.setTextSize(12);
请给我任何在按钮底部指定图像的参考方法。提前感谢。您可以使用
setCompoundDrawablesWithIntrinsicBounds()
完成此操作。建造商是:
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
对于您不希望分配图像资源的位置,您只需在其位置放置一个0。例如:
btnAddHotel.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.some_image_resource);
阅读更多关于
编辑:因为安卓
按钮继承了TextView
小部件的属性,上面指向TextView
文档的链接仍然有效。如果我正确理解了您的问题,您可以在按钮上使用setBackgroundDrawable()
:
btnAddHotel.setBackgroundDrawable(R.drawable.xxx);
您的意思是XML属性的Java等价物android:drawableBottom=“@drawable/…”
?是的。。。。我希望java代码等效于xml属性。。。