简体   繁体   English

是否有Android API来获取默认按钮按下/选择的颜色?

[英]Is there an Android API to get the default button pressed/selected colors?

I am trying to create a button that has a custom background in the default state, but that still uses the colors for the pressed/selected states that are particular to whatever device it is running on. 我正在尝试创建一个按钮,该按钮在默认状态下具有自定义背景,但仍然使用其所运行的任何设备特定的按下/选定状态的颜色。

From looking at the EditText source code, it appears that there is no such API, and that the pressed/selected colors are simply hard-coded: 从查看EditText源代码看,似乎没有这样的API,并且按下/选择的颜色只是硬编码:

public EditText(/*Context context, AttributeSet attrs, int defStyle*/) {
super(/*context, attrs, defStyle*/);

        StateListDrawable mStateContainer = new StateListDrawable();

        ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
        pressedDrawable.getPaint().setStyle(Paint.FILL);
        pressedDrawable.getPaint().setColor(0xEDEFF1);

        ShapeDrawable focusedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
        focusedDrawable.getPaint().setStyle(Paint.FILL);
        focusedDrawable.getPaint().setColor(0x5A8AC1);

        ShapeDrawable defaultDrawable = new ShapeDrawable(new RoundRectShape(10,10));
        defaultDrawable.getPaint().setStyle(Paint.FILL);
        defaultDrawable.getPaint().setColor(Color.GRAY);

        mStateContainer.addState(View.PRESSED_STATE_SET, pressedDrawable);
        mStateContainer.addState(View.FOCUSED_STATE_SET, focusedDrawable);
        mStateContainer.addState(StateSet.WILD_CARD, defaultDrawable);

        this.setBackgroundDrawable(mStateContainer);
}

Can anyone verify that it is not possible for an app to know what the pressed/selected colors are on the current device? 任何人都可以验证应用程序无法知道当前设备上按下/选择的颜色是什么吗?

If there is no way for an app to figure out what the pressed/selected colors are, then there is no way to create a Selector for a button that will match the UI across different devices. 如果应用无法确定按下/选择的颜色是什么,则无法为不同设备上的UI匹配的按钮创建选择器。

It seems that Android must provide a way to find out what these colors are so that apps can use colors that are consistent with the UI of the device they happen to be running on. 似乎Android必须提供一种方法来找出这些颜色是什么,以便应用程序可以使用与它们碰巧运行的设备的UI一致的颜色。

Thanks! 谢谢!

-Tom B. -Tom B.

Follow-up, in response to CaseyB's comment, I tried the following: 跟进,针对CaseyB的评论,我尝试了以下方法:

StateListDrawable d = new StateListDrawable();
d.addState(android.R.attr.state_enabled, getResources().getDrawable(R.drawable.my_custom_background));
myButton.setBackgroundDrawable(d);

but it causes the button to use the custom background for all states. 但它会使按钮为所有状态使用自定义背景。 There doesn't appear to be a way to tell the StateListDrawable to use the default drawables for the pressed/selected states. 似乎没有办法告诉StateListDrawable使用默认的drawables来处理按下/选择的状态。

最简单的方法是为你的按钮创建一个自定义样式,继承自android:style/Widget.Button并将android:background添加到你的自定义样式中。

你应该使用有状态的drawable,并让它使用这些状态的默认drawable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android的三个按钮状态(已按下,未按下和默认) - Android three button states (pressed, not pressed and default) 如何在Android中修改默认按钮状态而不影响按下状态和选定状态? - How to modify the default button state in Android without affecting the pressed and selected states? 如何在Android上获取默认颜色 - How to get the default colors on Android Android:在自定义按钮中使用默认的突出显示颜色 - Android: Use default highlight colors in a custom button 在Android中按下提交按钮时使用自定义适配器从列表视图中获取选定的项目 - Get selected items from listview using custom adapter on submit button pressed in Android 将默认Android橙色设为自定义按钮的焦点,按下状态,选中状态 - Make default Android orange as focused,pressed,selected state of a custom buttom 无法在 android 中按下蓝牙按钮 - Unable to get bluetooth button pressed in android 有没有办法让 Android 按钮按下 state? - Is there a way to get the Android button pressed state? Android:在选择,聚焦或按下按钮时重复执行dispatchKeyEvent - Android: Repeat a dispatchKeyEvent while while a button is selected, focused, or pressed Android获取主题颜色,禁用按钮 - Android get theme colors, disabled button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM