【问题标题】:Dynamic Color Change of Xml StyleXml 样式的动态颜色变化
【发布时间】:2017-05-10 12:24:58
【问题描述】:

我正在尝试制作一个形状,该形状会根据列表视图中项目的主题而变化。这是我需要做的;我的圆圈区域周围有一个细边框

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

这是 Layout 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

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

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

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

最后我的方法返回了我

我该如何解决?

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

【问题讨论】:

    标签: android xml dynamic colors styles


    【解决方案1】:

    你只需要将内部布局也设为圆形。

    你有太多选择:

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

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

    【讨论】:

    • 我解决了我的需求。你描述的方式也从我的脑海中消失了谢谢。我创建了一些圆形图像并将它们导入到我的drawable中,因此我注入了图像的颜色,但想知道是否有aw yo control xml样式会更有效。
    猜你喜欢
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多