Android以编程方式更改颜色描边(边框)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android以编程方式更改颜色描边(边框),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2464字,纯文字阅读大概需要4分钟。
内容图文
我尝试改变颜色“笔画”,通过“GradientDrawable”但不起作用.
另外,我不知道如何获取id中风,并且只改变笔画(我看到谷歌,所有例子都失败了)
我的XML项目
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/selectable_kachel_shape" >
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<stroke android:width="11dp" android:color="#ff00ffff"/>
<gradient
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF"
android:type="sweep" />
</shape>
</item>
</layer-list>
GradientDrawable gd = (GradientDrawable) circleProgress.getBackground();
gd = (GradientDrawable) circleProgress.getBackground();
gd.setColor(Color.RED);
android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable
这里有作品,但是…不能得到我的物品
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
circleProgress.setBackgroundDrawable(gd);
在这里更新工作,但不要改变我的中风(我用身份但是我没有工作)
只能使用ID项目.
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
shape.setColor(this.getResources().getColor(android.R.color.background_dark));
circleProgress.setBackgroundDrawable(shape);
解决方法:
除此之外,您可以使用任何布局并将xml应用为选择器,对于该布局,您可以通过GradientDrawable应用动态背景.
例如 ,
main.xml中
<RelativeLayout
android:orientation="vertical"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shapes"/>
那你的xml,shapes.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/selectable_kachel_shape" >
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<stroke android:width="11dp" android:color="#ff00ffff"/>
<gradient
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF"
android:type="sweep" />
</shape>
</item>
</layer-list>
并在您的活动中编写以下代码.
RelativeLayout mainLayout= (RelativeLayout ) findViewById(R.id.mainLayout);
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
mainLayout.setBackgroundDrawable(gd);
有关更多GradientDrawable,请使用链接http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
希望这会对你有所帮助.
内容总结
以上是互联网集市为您收集整理的Android以编程方式更改颜色描边(边框)全部内容,希望文章能够帮你解决Android以编程方式更改颜色描边(边框)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。