LayerDrawable以编程方式

编程入门 行业动态 更新时间:2024-10-11 19:15:01
本文介绍了LayerDrawable以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于我的应用程序的颜色主题是动态的,所以我只能使用颜色和shapedrawables创建背景可绘制对象, 我想构建一个可绘制颜色和形状可绘制的edittext背景,如下所示. 但是我想以编程方式做到这一点

Since my application's color theme is dynamic i can only create background drawables using colors and shapedrawables, i want to build a edittext background drawable with colors and shapes as shown below. But i want to do this programatically

如何通过编程方式构建相同的可绘制对象?

How to build this same drawable programatically?

<item> <shape> <solid android:color="@android:color/yellow" /> </shape> </item> <!-- main color --> <item android:bottom="1dp" android:left="1dp" android:right="1dp"> <shape> <solid android:color="@android:color/white" /> </shape> </item> <!-- draw another block to cut-off the left and right bars --> <item android:bottom="10dp"> <shape> <solid android:color="@android:color/white" /> </shape> </item>

这就是我尝试过的......

this is what i tried....

GradientDrawable border = new GradientDrawable(); border.setShape(GradientDrawable.RECTANGLE); border.setColor(Color.WHITE); GradientDrawable background = new GradientDrawable(); background.setShape(GradientDrawable.RECTANGLE); background.setColor(Color.YELLOW); GradientDrawable clip = new GradientDrawable(); clip.setShape(GradientDrawable.RECTANGLE); border.setColor(Color.WHITE); Drawable[] layers = {background, border, clip}; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(0, 0, 0, 0, 0); layerDrawable.setLayerInset(1, 1, 0, 1, 1); layerDrawable.setLayerInset(2, 0, 0, 0, 10);

但是结果不一样....请帮助....!

but the result is different....please help....!

推荐答案

我终于使它起作用了.我没有使用GradientDrawable,而是使用了ShapeDrawable.

I finally got it working. Instead of using GradientDrawable I used ShapeDrawable.

通过将此LayerDrawable设置为EditText背景,您可以重新生成默认的EditText 自定义颜色的样式.

By setting this LayerDrawable as an EditText background you can regenerate default EditText styles with custom colors.

ShapeDrawable border = new ShapeDrawable(); border.getPaint().setColor(Color.WHITE); ShapeDrawable background = new ShapeDrawable(); background.getPaint().setColor(Color.BLACK); ShapeDrawable clip = new ShapeDrawable(); clip.getPaint().setColor(Color.WHITE); Drawable[] layers = {background, border, clip}; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(0, 0, 0, 0, 0); layerDrawable.setLayerInset(1, 1, 0, 1, 1); layerDrawable.setLayerInset(2, 0, 0, 0, 10);

更多推荐

LayerDrawable以编程方式

本文发布于:2023-11-02 09:54:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   LayerDrawable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!