【问题标题】:Programmatically adding stroke to buttons以编程方式将笔划添加到按钮
【发布时间】:2017-07-19 02:39:07
【问题描述】:

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

我想要做的是让一个按钮默认有黑色边框。然后按下后颜色会变为红色。

【问题讨论】:

    标签: java android android-layout android-button


    【解决方案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());
    }
    

    【讨论】:

      【解决方案2】:

      我认为您真正需要的是创建一个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

      【讨论】:

        【解决方案3】:

        最简单的方法是将 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()
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-28
          • 1970-01-01
          • 2012-06-19
          • 2021-10-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多