Android 具有不同颜色和相同样式的按钮

Android 具有不同颜色和相同样式的按钮,android,button,colors,styles,Android,Button,Colors,Styles,我正在开发一个应用程序,其中包含一些通过layout.xml定义的按钮,如下所示 <?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:b

我正在开发一个应用程序,其中包含一些通过layout.xml定义的按钮,如下所示

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/largebutton" >
</Button>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_active_start" android:endColor="@color/menu_button_active_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_focused_start" android:endColor="@color/menu_button_focused_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_focused_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>
 .....
</selector>
List<Colors> colors = downloadColorsFromWeb();

Button b = new Button;
b.setDrawable(drawable.with(colors));
TabButton b = new TabButton(context, null);

b.setNormalState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.RED, Color.CYAN }));
b.setSelectedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLUE }));
b.setFocusedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.GREEN }));
b.setPressedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLACK }));

@可拉伸/大尺寸按钮看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/largebutton" >
</Button>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_active_start" android:endColor="@color/menu_button_active_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_focused_start" android:endColor="@color/menu_button_focused_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_focused_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>
 .....
</selector>
List<Colors> colors = downloadColorsFromWeb();

Button b = new Button;
b.setDrawable(drawable.with(colors));
TabButton b = new TabButton(context, null);

b.setNormalState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.RED, Color.CYAN }));
b.setSelectedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLUE }));
b.setFocusedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.GREEN }));
b.setPressedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLACK }));

.....
所有属性(如填充、笔划、半径)都相同,但渐变颜色处于不同状态除外。我的问题是我的应用程序必须有更多的样式。您可以想象,当您有一个颜色列表时,当您选择一个应用程序时,所有颜色都会更改为选定的颜色。因此,如果您有20种颜色,那么20种不同的XML不是正确的方法

所有android:State的startColor和endColor值都是从web下载并保存到DB的,我不知道有多少

有什么方法可以实现这种行为吗?我搜索了所有的论坛,大多数答案都是不可能的。我找到了一个覆盖colors.xml的“解决方案”,但对我来说似乎不是最好的解决方案

所以我的问题是,我可以在colors.xml中动态更改颜色吗?类似这样的内容

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/largebutton" >
</Button>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_active_start" android:endColor="@color/menu_button_active_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_focused_start" android:endColor="@color/menu_button_focused_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_focused_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>
 .....
</selector>
List<Colors> colors = downloadColorsFromWeb();

Button b = new Button;
b.setDrawable(drawable.with(colors));
TabButton b = new TabButton(context, null);

b.setNormalState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.RED, Color.CYAN }));
b.setSelectedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLUE }));
b.setFocusedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.GREEN }));
b.setPressedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLACK }));
List colors=downloadColorsFromWeb();
按钮b=新按钮;
b、 可设置可绘制(可绘制。带有(颜色));
提前谢谢大家


nosko.

您可能会为下载的每种颜色动态生成一个可绘制的颜色。检查GradientDrawable类。我认为您可以在初始化期间提供开始/结束颜色,然后设置笔划和角半径属性。但是你得自己去弄清楚填充物。我不确定

创建绘图表后,可以在按钮的
setBackgroundDrawable

编辑:设置按钮的填充可能会起作用


编辑2:你可以
设置状态
到可绘制状态,但我不知道如何为按钮的每个状态设置不同的背景可绘制状态。

谢谢@stan0的回复,它帮助了很多,尤其是GradientDrawable类。

我编写了一个简单的类,它创建按钮,并可以根据其状态设置样式。也许对某人有帮助:)

像这样使用它

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/largebutton" >
</Button>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_active_start" android:endColor="@color/menu_button_active_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient android:startColor="@color/menu_button_focused_start" android:endColor="@color/menu_button_focused_end" android:angle="270" />
            <stroke android:width="@dimen/largebutton_stroke" android:color="@color/menu_button_focused_stroke" />
            <corners android:radius="@dimen/largebutton_radius" />
            <padding android:left="@dimen/largebutton_padding_leftright" android:top="@dimen/largebutton_padding_topbottom" android:right="@dimen/largebutton_padding_leftright" android:bottom="@dimen/largebutton_padding_topbottom" />
        </shape>
    </item>
 .....
</selector>
List<Colors> colors = downloadColorsFromWeb();

Button b = new Button;
b.setDrawable(drawable.with(colors));
TabButton b = new TabButton(context, null);

b.setNormalState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.RED, Color.CYAN }));
b.setSelectedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLUE }));
b.setFocusedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.GREEN }));
b.setPressedState(new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.YELLOW, Color.BLACK }));