我需要将描边颜色更改为用户定义的颜色。与国家无关

发布于 2024-10-13 07:19:40 字数 876 浏览 8 评论 0原文

我需要从应用程序更改描边颜色。用户可以更改背景颜色,因此我还需要让他们更改按钮的笔划(轮廓)。由于它已经在可绘制对象中设置(下面的示例),我还没有找到更改此设置的方法。似乎所有其他类似的问题都只是说使用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风吹雪碎 2024-10-20 07:19:40

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>

那么您可以更改

a.描边颜色:

GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.mutate(); // only change this instance of the xml, not all components using this xml
drawable.setStroke(3, Color.RED); // set stroke width and stroke color 

Kotlin 扩展:

fun Drawable.setStroke(color: Color, width: Int) {
    (this as? GradientDrawable)?.apply {
        mutate()
        setStroke(color, width)
    }
}

b.纯色:

GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); // set solid color

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>

那么您可以通过按位置获取单独的可绘制对象来更改各个项目属性。

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

现在更改描边或纯色:

//solid color
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);

//stroke
gradientDrawableChecked.setStroke(1, Color.RED);
gradientDrawableUnChecked.setStroke(1, Color.BLUE);

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.mutate(); // only change this instance of the xml, not all components using this xml
drawable.setStroke(3, Color.RED); // set stroke width and stroke color 

Kotlin extension:

fun Drawable.setStroke(color: Color, width: Int) {
    (this as? GradientDrawable)?.apply {
        mutate()
        setStroke(color, width)
    }
}

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);
沧桑㈠ 2024-10-20 07:19:40

我需要一种方法来更改任何 GradientDrawable 的笔划颜色,而无需知道笔划的宽度。我的目标是使用 Drawable.setTint 来实现此目的。我必须在我的 shape xml 中添加一个透明的 solid 元素才能使其正常工作:

<!-- 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>
// StrokeView.kt
setBackgroundResource(R.drawable.stroke_background)
// Set the color of your stroke
drawable.setTint(Color.BLUE)

在您的情况下,因为您同时拥有 solid描边,那么您需要使用图层列表,其中描边添加在实体之后(以便将其绘制在顶部)。这将允许您在实体和笔划上设置不同的颜色,而无需提前知道笔划宽度是多少:

<!-- stroke_solid_background.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/solid_background" />
    <item android:drawable="@drawable/stroke_background" />
</layer-list>
// 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)
}

I needed a way to change the stroke color of any GradientDrawable without knowing the width of the stroke. My goal was to do this using Drawable.setTint. I had to add a transparent solid element to my shape xml to get it to work:

<!-- 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>
// StrokeView.kt
setBackgroundResource(R.drawable.stroke_background)
// Set the color of your stroke
drawable.setTint(Color.BLUE)

In your case, since you have both a solid and stroke, then you'll need to use a layer-list, where the stroke is added AFTER the solid (so that it's drawn on top). This will let you set different colors on the solid and the stroke without knowing in advance what the stroke width is:

<!-- stroke_solid_background.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/solid_background" />
    <item android:drawable="@drawable/stroke_background" />
</layer-list>
// 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)
}
落叶缤纷 2024-10-20 07:19:40

请查看 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>

您可以在运行时修改它,例如:

 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);

Please look at the LayerDrawable because it created from your XML and used at runtime.

Here is a Demo Example:

<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>

You can modify it at runtime like:

 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);
秋意浓 2024-10-20 07:19:40

使用透明 (#00000000) 实心创建背景(res/drawable 中的文件):

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

    <corners android:radius="4dp" />

    <stroke
        android:width="1dp"
        android:color="#FF0000" />

    <solid android:color="#00000000" />

</shape>

将此文件设置为背景:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number" />

现在,使用 android:backgroundTint 您可以更改边框的颜色。


例如:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number"
    android:backgroundTint="#FF00FF" />

#FF00FF

或:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number"
    android:backgroundTint="#00FF00" />

#00FF00

Create background (file in res/drawable) with transparent (#00000000) solid:

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

    <corners android:radius="4dp" />

    <stroke
        android:width="1dp"
        android:color="#FF0000" />

    <solid android:color="#00000000" />

</shape>

set this file as background:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number" />

and now, using android:backgroundTint you can change color of your border.


For example:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number"
    android:backgroundTint="#FF00FF" />

#FF00FF

or:

<View   
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_enter_number"
    android:backgroundTint="#00FF00" />

#00FF00

阿楠 2024-10-20 07:19:40

尝试使用 StateList(而不是 ColorStateList)。看一下:
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList

您还可以以编程方式创建一个 ShapeDrawable(或示例中的 RoundRectShape),然后调用按钮的 设置背景可绘制

Try using StateLists (as opposed to ColorStateList). Take a look:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

You can also create a ShapeDrawable (or a RoundRectShape in your example) programmatically, and then call the button's setBackgroundDrawable

土豪 2024-10-20 07:19:40

我在在运行时更改形状边框颜色中回答了类似的问题,

它就像一样f20k 提出的解决方案,但在我的例子中,可绘制对象是 GradientDrawable 而不是 ShapeDrawable。

看看是否有效...

I answered a similar question in Change shape border color at runtime

Its like the same solution proposed by f20k but in my case the drawable was a GradientDrawable instead of a ShapeDrawable.

see if it works...

眼眸印温柔 2024-10-20 07:19:40

也许他们指的是颜色状态列表,它允许您更改基于按钮是否被按下/聚焦/启用/等的颜色

Perhaps they are referring to Color State Lists which allows you to change the color based on whether the button was pressed/focused/enabled/etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文