Kar*_*ick 2 android border button
我有一个按钮,没有任何只有背景颜色的文字.如果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);
}
});
Run Code Online (Sandbox Code Playgroud)
Kar*_*ick 14
找到解决方案
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)
使用这些值并设置为按钮背景drawable.现在,按钮看起来是MAGENTA颜色的边框.