以编程方式向按钮添加描边
Programmatically adding stroke to buttons
有什么方法可以将 stroke/outline 添加到 java 而不是 XML 的按钮?我一直在努力研究它,我遇到的一切都是 XML。
我想要做的是让按钮默认带有黑色边框。然后在按下后颜色会变成红色。
我认为您真正需要的是创建一个 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
在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());
}
最简单的方法是将 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()
}
有什么方法可以将 stroke/outline 添加到 java 而不是 XML 的按钮?我一直在努力研究它,我遇到的一切都是 XML。
我想要做的是让按钮默认带有黑色边框。然后在按下后颜色会变成红色。
我认为您真正需要的是创建一个 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
在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());
}
最简单的方法是将 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()
}