我有一个按钮,没有任何文本,只有背景颜色。如果onClick()
是按钮,我需要在没有xml
规范的情况下设置按钮边框。我尝试将渐变矩形形状作为drawable
按钮的背景,这对于我的布局来说并不灵活。
如何为按钮设置特定颜色的边框?
这是我的代码。
Button btnBlackColor=new Button(this);
int mButtonWidth=100;
btnBlackColor.setWidth(mButtonWidth);
btnBlackColor.setHeight(mButtonWidth);
btnBlackColor.setBackgroundColor(Color.BLACK);
btnBlackColor.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
GradientDrawable btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLUE,Color.LTGRAY});
btnShape.setCornerRadius(0);
btnShape.setSize(mButtonWidth, mButtonWidth);
btnShape.setBounds(10, 10, mButtonWidth, mButtonWidth);
ClipDrawable btnClip = new ClipDrawable(btnShape, Gravity.LEFT,ClipDrawable.HORIZONTAL);
btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLACK, Color.DKGRAY});
btnShape.setCornerRadius(0);
btnShape.setSize(mButtonWidth, mButtonWidth);
btnShape.setBounds(5, 5, mButtonWidth, mButtonWidth);
LayerDrawable btnLayer= new LayerDrawable(new Drawable[]{btnShape, btnClip});
btnBlackColor.setBackgroundDrawable(btnLayer);
}
});