为不同的按钮

时间:2017-07-11 18:12:07

标签: android xml

我正在使用带有以下代码的xml文件,只要点击我的应用图标,就可以为我的某个按钮设置渐变和波纹。

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
 android:color="#c20586">
      <item>
        <shape android:shape="oval">
        <gradient android:angle="135" android:startColor="#FFFF66" 
                  android:endColor="#000000" />
       </shape>
     </item>

我的问题是: 我有6个这样的按钮。拥有6个不同的xml文件是一个好习惯(每个文件都需要不同的颜色)?

如果没有,如何使用单个xml文件?我最好的猜测是java中的oncreate,但是怎么样?

2 个答案:

答案 0 :(得分:0)

如果您愿意,可以在代码中执行此操作。

在XML中创建按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vzw.www.myapplication.MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button2"/>

</LinearLayout>

从那里你可以做到这一点......

Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);

    int[] colors = {Color.parseColor("#008000"), Color.parseColor("#ADFF2F")};

    GradientDrawable gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, colors);

    gradientDrawable.setCornerRadius(0f);

    button2.setBackground(gradientDrawable);

资源:https://android--code.blogspot.in/2015/01/android-button-gradient-color.html

答案 1 :(得分:0)

请使用此代码解决您的问题。

int[] colors = {Color.parseColor("#CCCCCC"), Color.parseColor("#FF0000")};

    GradientDrawable gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT, colors);

    gradientDrawable.setCornerRadius(0f);
    button1.setBackground(getAdaptiveRippleDrawable(Color.WHITE,Color.BLACK,gradientDrawable));

并实施这些方法。

public static Drawable getAdaptiveRippleDrawable(
        int normalColor, int pressedColor,Drawable drawable) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new RippleDrawable(ColorStateList.valueOf(pressedColor),
                drawable, getRippleMask(normalColor));
    } else {
        return getStateListDrawable(normalColor, pressedColor);
    }
}

private static Drawable getRippleMask(int color) {
    float[] outerRadii = new float[8];
    // 3 is radius of final ripple,
    // instead of 3 you can give required final radius
    Arrays.fill(outerRadii, 3);

    RoundRectShape r = new RoundRectShape(outerRadii, null, null);
    ShapeDrawable shapeDrawable = new ShapeDrawable(r);
    shapeDrawable.getPaint().setColor(color);
    return shapeDrawable;
}

public static StateListDrawable getStateListDrawable(
        int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_focused},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_activated},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{},
            new ColorDrawable(normalColor));
    return states;
}