我有一个按钮,没有任何只有背景颜色的文字。在按钮的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);
}
});
答案 0 :(得分:14)
找到解决方案。
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
使用这些值并设置为按钮背景可绘制。现在,该按钮看起来是带有MAGENTA颜色的边框。
答案 1 :(得分:0)
在xml背景中使用选择器。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><!-- pressed -->
<shape>...</shape>
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
burton的每个状态都可能是一个形状,看看你可以修改的属性:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
通过代码,您可以使用StateListDrawable
制作选择器,对于形状使用DrawableShape
,请查看以下链接:
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html