标签:android android-drawable android-ui shapedrawable
我在图层列表中有一个形状,我的目标是在运行时以编程方式更改形状的颜色.我有HEX代码的字符串,我使用Color.parseColor()来解析它,我传递给setColor方法.每当我运行应用程序时,它会显示不同的颜色,我希望.
这是我的XML文件代码:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/lvbg"
android:top="1dp">
<shape
android:id="@+id/listview_background"
android:shape="rectangle" >
<size
android:height="220dp"
android:width="600dp" >
</size>
<solid android:color="@android:color/black"></solid>
<corners android:radius="15dp" />
</shape>
</item>
</layer-list>
这是我在CustomAdapter中的代码:
convertView = mInflater.inflate(R.layout.student_info_selection_fragment_icon, null);
holder = new ViewHolder();
holder.collegeBG=(LayerDrawable)convertView.getResources().getDrawable(R.drawable.rectangle);
holder.bg = (GradientDrawable)holder.collegeBG.findDrawableByLayerId(R.id.lvbg);
String color = "#FF" + rowItem.getCollegeColor();
holder.bg.setColor(Color.parseColor(color));
例如,当我放#FF1D0A63时,我会变成黑色或棕色,颜色完全不同.
谢谢
解决方法:
我仍然不知道问题是什么,但我意识到当我在xml中使用layer-list并将其作为背景分配给视图时,并尝试在layer-list中更改形状的颜色我遇到了这个问题.
所以我解决了将背景形状与图层列表分开的问题.
我将背景形状放到另一个文件中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_background"
android:shape="rectangle" >
<size
android:height="220dp"
android:width="600dp" >
</size>
<solid android:color="@android:color/black" > </solid>
<corners android:radius="15dp" />
</shape>
我将其指定为TextView的背景.
我使用图层列表的原因是组合2-3个形状并使它们渐变并给它们圆角.相反,我使用了View和TextView并将形状分配给它们作为背景并且它起作用.
这是我的新CustomAdapter:
convertView = mInflater.inflate(R.layout.student_info_selection_fragment_icon, null);
holder = new ViewHolder();
holder.tvBackground = (TextView) convertView.findViewById(R.id.tvSelectionCollegeBackground);
GradientDrawable background = (GradientDrawable) holder.tvBackground.getBackground();
String color = "#FF" + rowItem.getCollegeColor();
background.setColor(Color.parseColor(color));
holder.tvBackground.setBackground(background);
标签:android,android-drawable,android-ui,shapedrawable 来源: https://codeday.me/bug/20190625/1283992.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。