Android的按键边框动态 [英] Android button border dynamically
本文介绍了Android的按键边框动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮,没有只与背景颜色的任何文字。在的onClick按钮()
事件中,我需要设置按钮边框没有 XML
规范。我试图梯度长方形为背景绘制
来的按钮,这是不灵活了我的布局。
如何设置边框与特定颜色的按钮?
下面是我的code。
按钮btnBlackColor =新按钮(本);
INT mButtonWidth = 100;
btnBlackColor.setWidth(mButtonWidth);
btnBlackColor.setHeight(mButtonWidth);
btnBlackColor.setBackgroundColor(Color.BLACK); btnBlackColor.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
{
GradientDrawable btnShape =新GradientDrawable(Orientation.BOTTOM_TOP,新的INT [] {Color.BLUE,Color.LTGRAY});
btnShape.setCornerRadius(0);
btnShape.setSize(mButtonWidth,mButtonWidth);
btnShape.setBounds(10,10,mButtonWidth,mButtonWidth);
ClipDrawable btnClip =新ClipDrawable(btnShape,Gravity.LEFT,ClipDrawable.HORIZONTAL); btnShape =新GradientDrawable(Orientation.BOTTOM_TOP,新的INT [] {Color.BLACK,Color.DKGRAY});
btnShape.setCornerRadius(0);
btnShape.setSize(mButtonWidth,mButtonWidth);
btnShape.setBounds(5,5,mButtonWidth,mButtonWidth);
LayerDrawable btnLayer =新LayerDrawable(新绘制对象[] {btnShape,btnClip}); btnBlackColor.setBackgroundDrawable(btnLayer);
}
});
解决方案
找到解决方案。
GradientDrawable绘=新GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5,Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(绘制);
使用这些值并设置按钮的背景绘制。现在,按钮的外观与边境洋红色。
I am having a button, without any text only with background color. In onClick()
event of the button I need to set the button border without the xml
specification. I tried gradient rectangle shape as background drawable
to the button which is not flexible for my layout.
How can I set border with specific color to the button?
Here is my code.
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);
}
});
解决方案
Found solution.
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
Use these values and set to button background drawable. Now, the button looks with border in MAGENTA color.
这篇关于Android的按键边框动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文