android - xml样式动态换色

标签 android xml dynamic colors styles

我正在尝试制作一个形状,它会根据 ListView 中项目的主题而变化。这是我需要做的;我的圈子区域周围的细边框 enter image description here

我的 xml 样式是cw_full_oval

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:bottomLeftRadius="100dp"
        android:bottomRightRadius="100dp"
        android:topLeftRadius="100dp"
        android:topRightRadius="100dp" />
    <!--solid android:color="" / inner side full color-->
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    <size
        android:width="100dp"
        android:height="100dp" />
    <stroke
        android:width="1dp"
        android:color="@color/red_200" />
</shape>
enter code here

这是布局 xml 的主要部分,我需要它的形状

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="15dp"
                    android:background="@drawable/cw_full_oval"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_margin="10dp"
                        android:background="@drawable/cw_full_oval"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <LinearLayout
                            android:id="@+id/lin_lay_item_etkinlik_gl"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>

当我从 xml 设置样式时,我的线性布局图像如下所示

enter image description here

在我的适配器类中,我试图给出我的方法、主题名称和当前的线性布局

String nameOfObj = currentActivity.getName();
Util.getInstance(context).setColor(nameOfObj,holder.lin_lay_etkinlik);

这个方法我知道的还不够,只是想描述一下我需要什么

 public void setColor(String name, LinearLayout linlay){

        switch (name){
            case "Sinema":
                linlay.setBackgroundResource(R.color.sinema);
                break;
            case "Piknik":
                linlay.setBackgroundResource(R.color.piknik);
                break;
            case "Futbol":
                linlay.setBackgroundResource(R.color.futbol);
                break;
            case "Gezi":
                linlay.setBackgroundResource(R.color.gezi);
                break;
            case "Yemek":
                linlay.setBackgroundResource(R.color.yemek);
                break;
            case "Sohbet":
                linlay.setBackgroundResource(R.color.sohbet);
                break;
            case "Cay":
                linlay.setBackgroundResource(R.color.cay);
                break;
            case "Toplantı":
                linlay.setBackgroundResource(R.color.sinema);
                break;
            default:
                linlay.setBackgroundResource(R.color.genel);
                break;
        }
    }

最后我的方法返回我 enter image description here

我该如何解决?

好吧,我不必遵守我的策略,任何其他实用的解决方案都会被应用。

最佳答案

你只需要将内部布局也做成圆形即可。

你还有很多选择:

1-使用您想要的填充颜色创建圆形可绘制对象。由于您没有将可绘制对象制作成完美的圆形(如果愿意,您只需要将可绘制对象的形状设置为圆形),您需要在新可绘制对象中进行相同的配置。

2-为每种颜色制作一个drawable,并在里面制作填充颜色,你已经完成了,你只需要取消注释填充颜色部分。

关于android - xml样式动态换色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324449/

相关文章:

Android ListView 滚动到

android - Paypal 服务未启动(沙盒帐户),出现以下错误

android - 使用/strings.xml覆盖res/values/strings.xml文件从服务器android下载到sd卡

android - 注册一个应用程序以在 Android 中使用对话框显示在“完成操作”中

java - XSD XML 架构中 maxOccurs 的最大整数是多少?

c# - 如何添加xml命名空间

css - 使占位符 div 可动态重叠

java - 从 Jaxb 1 更新到 Jaxb 2

mysql - DATE 在动态之间

javascript - 使用 JavaScript 向 Quill 编辑器添加自定义下拉工​​具