以编程方式更改图层列表Drawable项的底部属性 [英] Change bottom property of item of layer-list Drawable programmatically

查看:338
本文介绍了以编程方式更改图层列表Drawable项的底部属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建创建底部笔触的LayerDrawable,但是我不知道如何给层(Drawablw)的底部边距.

I am creating a LayerDrawable that creates bottom stroke but i dont know how to give bottom margin of a layer(Drawablw).

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:bottom="2dp">
      ..
        </item>
    </layer-list>

我想像上面一样以编程方式设置下边距.

I want to set bottom margin like above programmatically.

到目前为止,我已经做到了:

So far i have done this:

Drawable[] list = new Drawable[2];
GradientDrawable strokeDrawable = new GradientDrawable(
    GradientDrawable.Orientation.TOP_BOTTOM, new int[] {
        strokeColor[0], strokeColor[0] });
GradientDrawable backgroundDrawable = new GradientDrawable(
    GradientDrawable.Orientation.TOP_BOTTOM, bgColor);
// Now how to set bottom margin to make border. 

list[0] = strokeDrawable;
list[1] = backgroundDrawable;

LayerDrawable layerDrawable = new LayerDrawable(list);

有人知道吗?

推荐答案

经过大量挖掘,我找到了解决方案,尽管它解决了我的问题,但这不是我想要的.

After a lot digging i found a solution, though it solved my problem but it is not what i was looking for.

我创建了一个可绘制的图层列表,并动态更改了其项目的颜色. 这是代码:

I created a layer-list drawable and changed its items color dynamically. Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/item_bottom_stroke" >
    <shape android:shape="rectangle">
        <solid android:color="#0096FF"/>
    </shape>
</item>
<item android:id="@+id/item_navbar_background" android:bottom="1dp" >
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF"/>
    </shape>
</item>

以下代码会在运行时修改上述drawable来更改其颜色.

Following code modifies above drawable at runtime to change its colors.

LayerDrawable layerDrawable = (LayerDrawable) v.getContext().getResources().getDrawable(R.drawable.layer_list_navigation_with_border);
GradientDrawable strokeDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_bottom_stroke);
strokeDrawable.setColor(strokeColor[0]);
GradientDrawable backgroundColor = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_navbar_background);
backgroundColor.setColors(bgColor);

发布了解决方案,以为某人可能会受益.

posted solution, thought someone might get benefited.

这篇关于以编程方式更改图层列表Drawable项的底部属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆