如何获得中性按钮的标准颜色

How to get a standard color of the neutral button

我正在使用样式为“?android:attr/buttonBarNeutralButtonStyle”的按钮

<Button
    style="?android:attr/buttonBarNeutralButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Sign in" />

我想获得它的颜色,这样我也可以将它与其他视图元素一起使用。

目前我正在读取它的颜色值是这样的:

int color;

View button = findViewById(R.id.passwordSigninButton);
if ((button != null) && (button instanceof Button))
  color = ((Button) button).getCurrentTextColor();
// -16738680

...而且效果很好。但我更愿意直接获取与适用样式关联的颜色,而不需要使用实际按钮,以防我想在布局中没有按钮的情况下使用它。

所以我尝试了这个方法:

TypedValue typedValue = new TypedValue();

getApplicationContext().getTheme().resolveAttribute(
    android.R.attr.buttonBarNeutralButtonStyle, typedValue, true);

TypedArray typedArray = context.obtainStyledAttributes(
    typedValue.data, new int[]{android.R.attr.textColor});

int color = typedArray.getColor(0, -1);
// -1

typedArray.recycle();

但是我得到的是 -1,这意味着我没有得到预期的颜色。

如何从 android.R.attr.buttonBarNeutralButtonStyle 样式中获取颜色?

我运行 上面给出的代码相同。我能够得到一个整数值。

输出:System.out:打印颜色整数值:-49023

import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TypedValue typedValue = new TypedValue();

        getApplicationContext().getTheme().resolveAttribute(
                android.R.attr.buttonBarNeutralButtonStyle, typedValue, true);

        TypedArray typedArray = this.obtainStyledAttributes(
                typedValue.data, new int[]{android.R.attr.textColor});

        int color = typedArray.getColor(0, -1);
        System.out.println("Printing the color int value: " + color);

        typedArray.recycle();
    }
}

下面的代码有不同的风格:

 getApplicationContext().getTheme().resolveAttribute(
                    android.R.attr.actionButtonStyle, typedValue, true);

 TypedArray typedArray = this.obtainStyledAttributes(
                    typedValue.data, new int[]{android.R.attr.colorForeground});

 int color = typedArray.getColor(0, -1);
 System.out.println("Printing the color int value: " + color);

输出:System.out:打印颜色整数值:-16777216


我正在使用 Android Studio 3.0.1 Build #AI-171.4443003,建于 2017 年 11 月 9 日 JRE: 1.8.0_152-release-915-b08 x86_64 JVM:JetBrains 的 OpenJDK 64 位服务器虚拟机 s.r.o Mac OS X 10.13.3

请参阅下面的直接推导更新。

您确定中性按钮文本颜色的方法试图复制 Android 确定颜色的步骤。这个处理是在幕后进行的,并且可以随着版本的不同而变化,正如我认为我们从对这个问题的回答中看到的那样。我相信 Android 开发人员可以随意更改底层实现,只要结果相同,因此,即使您可以获得今天有效的解决方案,它也很脆弱,明天可能会崩溃。

您确实有解决方案,但它涉及创建您想要避免的布局。我建议采用以下方法,我相信它可以满足您的目的,它会更加健壮,并且不需要布局文件。您表明您正在使用 android.support.v7.app.AppCompatDialog 创建 AppCompatButton 个按钮。

以下两行将为您提供中性按钮文本的颜色,无需布局或提取属性的复杂性。我已经在模拟器 运行 API 22 和 Samsung S7 运行 API 24 上进行了测试,这两个结果都与显式布局所见一致。

android.support.v7.widget.AppCompatButton button =
    new android.support.v7.widget.AppCompatButton(this, null,
    android.R.attr.buttonBarNeutralButtonStyle);
int color = button.getCurrentTextColor();

直接推导

虽然我认为上面的解决方案是最好的,但下面的代码将为所有 API 导出中性按钮的默认颜色。这种方法只是查找 textColor 属性,如果未定义,则查找 textAppearance 并查看那里定义的 textColor。看起来不同 APIs 之间的区别在于如何指定颜色。此解决方案可能足够稳健以承受后续更新,但警告买者

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.support.v7.widget.AppCompatButton button =
            new android.support.v7.widget.AppCompatButton(this, null, android.R.attr.buttonBarNeutralButtonStyle);
        Log.d("MainActivity", String.format("<<<< Button color = 0x%08X", button.getCurrentTextColor()));
        Log.d("MainActivity", String.format("<<<< Derived color = 0x%08X", getNeutralButtonColor(Color.WHITE)));
    }

    private int getNeutralButtonColor(int defaultColor) {
        TypedArray ta;
        int color = defaultColor;
        boolean colorFound = false;
        int textColorResId;
        final int baseAttr = R.attr.buttonBarNeutralButtonStyle;

        // Look for an explicit textColor attribute and use it if it is defined.
        ta = getTheme().obtainStyledAttributes(null, new int[]{android.R.attr.textColor}, baseAttr, 0);
        textColorResId = ta.getResourceId(0, -1);
        if (textColorResId == -1) { // try to get color if not resource id
            int type = ta.getType(0);
            if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
                color = ta.getColor(0, defaultColor);
                colorFound = true;
            }
        }
        ta.recycle();

        if (textColorResId == -1 && !colorFound) {
            // No color, yet. See if textAppearance is defined.
            ta = obtainStyledAttributes(null, new int[]{android.R.attr.textAppearance},
                                        baseAttr, 0);
            int textAppearanceId = ta.getResourceId(0, -1);
            if (textAppearanceId != -1) {
                // OK, textAppearance is defined. Get the embedded textColor.
                ta.recycle();
                ta = obtainStyledAttributes(textAppearanceId, new int[]{android.R.attr.textColor});
                textColorResId = ta.getResourceId(0, -1);
                if (textColorResId == -1) { // try to get color if not resource id
                    int type = ta.getType(0);
                    if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
                        color = ta.getColor(0, defaultColor);
                        colorFound = true;
                    }
                }
            }
            ta.recycle();
        }

        if (textColorResId != -1 && !colorFound) {
            ColorStateList colorStateList = AppCompatResources.getColorStateList(this, textColorResId);
            if (colorStateList != null) {
                color = colorStateList.getDefaultColor();
                colorFound = true; // in case needed later
            }
        }

        return color;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "MainActivity";
}

您可以在项目的值文件夹内创建 colors.xml 并定义要用于视图的颜色。例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="white">#FFFFFF</color>
 <color name="green">#008000</color>
 <color name="blue">#0000FF</color>
 <color name="black">#000000</color>
</resources>

然后在您的样式中,参考此文件中的颜色:

例如:

<style name="ButtonColor" parent="@android:style/Widget.Button">
   <item name="android:textColor">@color/blue</item>
</style> 

然后对于您想要颜色的任何其他元素,您不需要依赖视图,只需从此 colors.xml

访问
textView.setTextColor(getResources().getColor(R.color.blue));

但如果您想真正从样式中访问它:

// The attributes you want retrieved
int[] attrs = {android.R.attr.textColor, android.R.attr.text};

// Parse style
TypedArray typedArray  = obtainStyledAttributes(R.style.stylename, attrs);

// Get color from style
int textColor = typedArray.getColor(0, "default color");

//Check by priting
Log("Retrieved textColor as hex:", Integer.toHexString(textColor));

//recycle
typedArray.recycle()