以编程方式向按钮添加笔划

时间:2017-07-19 02:39:08

标签: java android android-layout android-button

有没有办法用java而不是XML为按钮添加笔画/轮廓?我一直在努力研究它,我遇到的一切都是XML。

我要做的是默认情况下按钮有黑色边框。然后在按下之后颜色会变为红色。

3 个答案:

答案 0 :(得分:1)

在Java中你可以这样实现,

    public Drawable getMyDrawable() {
        StateListDrawable states = new StateListDrawable();

//        states.addState(new int[]{
//                -android.R.attr.state_enabled,
//        }, getDisableDrawable());

        states.addState(new int[]{
                android.R.attr.state_focused, -android.R.attr.state_pressed,
        }, getDrawable(true));
        states.addState(new int[]{
                android.R.attr.state_focused, android.R.attr.state_pressed,
        }, getDrawable(true));
        states.addState(new int[]{
                -android.R.attr.state_focused, android.R.attr.state_pressed,
        }, getDrawable(true));
        states.addState(new int[]{
                android.R.attr.state_enabled
        }, getDrawable(false));

        return states;
    }

    public Drawable getDrawable(boolean pressed) {
        Drawable[] normalDrawable = new Drawable[2];
        normalDrawable[0] = getRectBorder(pressed);
        normalDrawable[1] = getRectBG();

        LayerDrawable layerDrawable = new LayerDrawable(normalDrawable);
        layerDrawable.setLayerInset(1, 2, 2, 2, 2);

        return layerDrawable.mutate();
    }

    public Drawable getRectBorder(boolean pressed) {
        RectShape rectShape = new RectShape();
        ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
        shapeDrawable.getPaint().setColor(pressed ? Color.RED : Color.BLACK);
        shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
        shapeDrawable.getPaint().setStrokeWidth(2);
        shapeDrawable.getPaint().setAntiAlias(true);
        shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
        return shapeDrawable.mutate();
    }

    public Drawable getRectBG() {
        RectShape rectShape = new RectShape();
        ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
        shapeDrawable.getPaint().setColor(Color.WHITE);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable.getPaint().setAntiAlias(true);
        shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
        return shapeDrawable.mutate();
    }

最后在你的按钮中你可以这样设置,

Button button = (Button) findViewById(R.id.button);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    button.setBackgroundDrawable(getMyDrawable());
} else {
    button.setBackground(getMyDrawable());
}

答案 1 :(得分:0)

我认为你真正需要的是创建一个selector并将其用作按钮背景

无需在java中处理你自己的案例

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_bg_normal"></item>

</selector>

并在按钮

中使用它
<Button
     android:id="@+id/button1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

有关详细信息,请参阅this question

答案 2 :(得分:0)

最简单的方法是将Button放在任何带有边距的布局中。保证金大小变为边框大小。然后当你点击时只需更改布局背景。

<LinearLayout android:orientation="vertical" 
    android:id="btn_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/black_alpha2">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:text="Click me"
    android:layout_margin="2dp"/>

如上所述。使用默认按钮背景时有点困难

然后在点击btn或需要更改的任何内容时更改容器布局背景

public void onClickBtn(View view){
  btnContainer.setBackground()
}