Mar*_*nop 58 android shape stroke
我需要从应用程序中更改笔触颜色.用户可以更改背景颜色,因此我还需要让他们更改按钮的笔触(轮廓).由于它已经在drawable(下面的示例)中设置,我还没有找到改变它的方法.看起来像所有其他问题只是说使用XML文件......但这并不能让我变得动态.感谢您的任何帮助!
我需要将笔触颜色更改为用户定义的颜色.与国家无关.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke
android:width="3dp"
android:color="@color/Dim_Gray" /> <<<<--- This is what I need to change
<padding android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
<corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp" android:topRightRadius="12dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
Ran*_*ngh 178
1.如果你有这样的"视图"的可绘制文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="3px"
android:color="@color/blue" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后你可以改变
一个.笔画颜色:
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setStroke(3, Color.RED); // set stroke width and stroke color
Run Code Online (Sandbox Code Playgroud)
湾 纯色:
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); // set solid color
Run Code Online (Sandbox Code Playgroud)
2.如果你有这样的"视图"的可绘制文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:id="@+id/buttonSelected">
<shape>
<solid android:color="@color/blue" />
<stroke android:width="1px" android:color="@color/blue" />
</shape>
</item>
<item android:state_checked="false" android:id="@+id/buttonNotSelected">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
<stroke android:width="1px" android:color="@color/blue" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过在该位置采用单独的可绘制对象来更改单个项目属性.
StateListDrawable drawable = (StateListDrawable)view.getBackground();
DrawableContainerState dcs = (DrawableContainerState)drawable.getConstantState();
Drawable[] drawableItems = dcs.getChildren();
GradientDrawable gradientDrawableChecked = (GradientDrawable)drawableItems[0]; // item 1
GradientDrawable gradientDrawableUnChecked = (GradientDrawable)drawableItems[1]; // item 2
Run Code Online (Sandbox Code Playgroud)
现在改变笔画或纯色:
//solid color
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);
//stroke
gradientDrawableChecked.setStroke(1, Color.RED);
gradientDrawableUnChecked.setStroke(1, Color.BLUE);
Run Code Online (Sandbox Code Playgroud)
请查看 LayerDrawable,因为它是根据 XML 创建并在运行时使用的。
这是一个演示示例:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:id="@+id/itemId" android:top="-4dp" android:right="-4dp" android:left="-4dp">
<shape>
<solid android:color="@android:color/transparent"/>
<stroke
android:width="1.5dp"
android:color="#06C1D7" >
</stroke>
<padding
android:bottom="-2dp"/>
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
您可以在运行时修改它,例如:
LayerDrawable layerDrawable = (LayerDrawable) getResources()
.getDrawable(R.drawable.only_one_bottom_line);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.itemId);
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.5f, getResources().getDisplayMetrics());
gradientDrawable.setStroke(px, getResources().getColor(R.color.theme_color));
tv_select_city_name.setBackground(layerDrawable);
Run Code Online (Sandbox Code Playgroud)
我需要一种在GradientDrawable不知道笔触宽度的情况下更改任何笔触颜色的方法。我的目标是使用Drawable.setTint。我必须在solid我的shapexml中添加一个透明元素才能使其正常工作:
<!-- stroke_background.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="4dp"
android:color="@android:color/white" />
<!-- Need transparent solid to get tint applied to only the stroke -->
<solid android:color="@android:color/transparent"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
// StrokeView.kt
setBackgroundResource(R.drawable.stroke_background)
// Set the color of your stroke
drawable.setTint(Color.BLUE)
Run Code Online (Sandbox Code Playgroud)
在您的情况下,由于您同时拥有solid和stroke,因此您需要使用layer-list,在笔划之后在实体之后添加笔画(以便将其绘制在顶部)。这将使您可以在实体和笔划上设置不同的颜色,而无需事先知道笔划宽度是什么:
// StrokeView.kt
setBackgroundResource(R.drawable.stroke_background)
// Set the color of your stroke
drawable.setTint(Color.BLUE)
Run Code Online (Sandbox Code Playgroud)
// StrokeSolidView.kt
setBackgroundResource(R.drawable.stroke_solid_background)
(drawable as LayerDrawable).apply {
// Set the color of your solid
getDrawable(0).setTint(Color.GREEN)
// Set the color of your stroke
getDrawable(1).setTint(Color.BLUE)
}
Run Code Online (Sandbox Code Playgroud)