我需要改变笔触颜色给用户定义的颜色。无关的国家 [英] I need to change the stroke color to a user defined color. Nothing to do with the state
问题描述
我需要改变从应用笔触颜色。用户能够改变背景颜色,所以我也需要让他们改变按钮的行程(轮廓)。作为其在绘制(下面的示例)中已设置我还没有找到一种方法来改变这种状况。好像所有的这样只是说使用XML文件中的其他问题....但是,这并不让我使其动态。感谢您的帮助!
我需要改变笔触颜色给用户定义的颜色。无关的状态。
<?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android> [固体机器人:颜色=#FFFFFFFF/>
<中风
机器人:宽=3DP
机器人:颜色=@色/ Dim_Gray/> <<<< ---这是我需要改变
<填充机器人:左=10dp
机器人:顶部=10dp
机器人:右=10dp
机器人:底部=10dp
/> <角落的android:bottomRightRadius =12dp机器人:bottomLeftRadius =12dp
机器人:topLeftRadius =12dp机器人:topRightRadius =12dp/>< /形状>
的 1。如果你有绘文件的视图像这样的
<?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android><角落的android:半径=5DP/>[固体机器人:色=@机器人:彩色/白/><中风
机器人:宽=的3px
机器人:颜色=@色/蓝色/>< /形状>
然后您可以更改
结果。笔触颜色:
GradientDrawable绘制=(GradientDrawable)view.getBackground();
drawable.setStroke(3,Color.RED); //设置笔划宽度和中风的颜色
结果
乙。纯色:
GradientDrawable绘制=(GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); //设置纯色
的 2。如果你有绘文件的视图像这样的
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:state_checked =真正的机器人:ID =@ + ID / buttonSelected>
<形状和GT;
[固体机器人:色=@色/蓝色/>
<行程机器人:宽=1px的机器人:颜色=@色/蓝色/>
< /形状>
< /项目>
<项目的android:state_checked =false的机器人:ID =@ + ID / buttonNotSelected>
<形状机器人:形状=矩形>
[固体机器人:色=@色/白/>
<行程机器人:宽=1px的机器人:颜色=@色/蓝色/>
< /形状>
< /项目>
< /选择>
然后就可以改变个人的项目由有立场采取的单独拖拉的对象属性。
StateListDrawable绘制=(StateListDrawable)view.getBackground();
DrawableContainerState DCS =(DrawableContainerState)drawable.getConstantState();
可绘制[] = drawableItems dcs.getChildren();
GradientDrawable gradientDrawableChecked =(GradientDrawable)drawableItems [0]; //第1项
GradientDrawable gradientDrawableUnChecked =(GradientDrawable)drawableItems [1]; //项目2
现在为更改行程或纯色:
//纯色
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);//行程
gradientDrawableChecked.setStroke(1,Color.RED);
gradientDrawableUnChecked.setStroke(1,Color.BLUE);
I need to change the stroke color from the app. The user is able to change the background color so I need to also let them change the stroke (outline) of the button. As its is already set in the drawable (sample below) I have not found a way to change this. Seems like all of the other questions like this just said to use the XML file.... but that doesnt let me make it dynamic. Thank you for any help!
I need to change the stroke color to a user defined color. Nothing to do with the state.
<?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>
1. If you have drawable file for a "view" like this
<?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>
Then you can change
a. Stroke color :
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setStroke(3, Color.RED); // set stroke width and stroke color
b. Solid color :
GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); // set solid color
2. If you have drawable file for a "view" like this
<?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>
Then you can change the individual item attributes by taking separate drawable objects by there positions.
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
now to change stroke or solid color :
//solid color
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);
//stroke
gradientDrawableChecked.setStroke(1, Color.RED);
gradientDrawableUnChecked.setStroke(1, Color.BLUE);
这篇关于我需要改变笔触颜色给用户定义的颜色。无关的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!