本文介绍了Android的:如何添加自定义按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,默认按钮都有其状态和背景图像之间的依赖关系如下:

For instance, the default button has the following dependencies between its states and background images:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true"
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
        android:drawable="@drawable/btn_default_normal_disable" />
</selector>

我如何定义我自己的自定义状态(水木清华像安卓state_cu​​stom ),这样的话我可以用它来动态地改变我的按钮的视觉外观

How can I define my own custom state (smth like android:state_custom), so then I could use it to dynamically change my button visual appearance?

推荐答案

通过@(泰德·霍普)表示该解决方案,但需要一点点修正:在选择时,该项目的国家需要一个应用程序:preFIX,否则充气将无法识别正确的命名空间,而将失败默默的;至少这是发生在我身上的东西。

The solution indicated by @(Ted Hopp) works, but needs a little correction: in the selector, the item states need an "app:" prefix, otherwise the inflater won't recognise the namespace correctly, and will fail silently; at least this is what happens to me.

请允许我在这里报告的整体解决方案,与一些更多的细节:

Allow me to report here the whole solution, with some more details:

首先,创建文件RES /价值/ attrs.xml:

First, create file "res/values/attrs.xml":

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="food">
        <attr name="state_fried" format="boolean" />
        <attr name="state_baked" format="boolean" />
    </declare-styleable>
</resources>

然后定义自定义类。例如,它可以是一类FoodButton,从类按钮而得。你必须实现一个构造函数;实现这一项,这似乎是一个使用的充气器:

Then define your custom class. For instance, it may be a class "FoodButton", derived from class "Button". You will have to implement a constructor; implement this one, which seems to be the one used by the inflater:

public FoodButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

在派生类的顶部:

private static final int[] STATE_FRIED = {R.attr.state_fried};
private static final int[] STATE_BAKED = {R.attr.state_baked};

另外,你的状态变量:

Also, your state variables:

private boolean mIsFried = false;
private boolean mIsBaked = false;

和一对夫妇的制定者:

public void setFried(boolean isFried) {mIsFried = isFried;}
public void setBaked(boolean isBaked) {mIsBaked = isBaked;}

然后重写功能onCreateDrawableState:

Then override function "onCreateDrawableState":

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
    if (mIsFried) {
        mergeDrawableStates(drawableState, STATE_FRIED);
    }
    if (mIsBaked) {
        mergeDrawableStates(drawableState, STATE_BAKED);
    }
    return drawableState;
}

最后,最精致的一块这个难题;选择确定,你将作为背景,你的部件使用StateListDrawable。这是文件RES /绘制/ food_button.xml:

Finally, the most delicate piece of this puzzle; the selector defining the StateListDrawable that you will use as a background for your widget. This is file "res/drawable/food_button.xml":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.mydomain.mypackage">
<item
    app:state_baked="true"
    app:state_fried="false"
    android:drawable="@drawable/item_baked" />
<item
    app:state_baked="false"
    app:state_fried="true"
    android:drawable="@drawable/item_fried" />
<item
    app:state_baked="true"
    app:state_fried="true"
    android:drawable="@drawable/item_overcooked" />
<item
    app:state_baked="false"
    app:state_fried="false"
    android:drawable="@drawable/item_raw" />
</selector>

请注意,应用程序:preFIX,而使用标准Android状态,你会使用preFIX机器人。 XML命名空间是一个正确的跨pretation由充气关键,取决于项目中,要添加属性的类型。如果它是一个应用程序,替换的 com.mydomain.mypackage 的与您的应用程序的实际包名称(应用程序名称除外)。如果它是一个库,你必须使用http://schemas.android.com/apk/res-auto(并使用工具R17或更高版本),或者你会得到运行时错误。

Notice the "app:" prefix, whereas with standard android states you would have used prefix "android:". The XML namespace is crucial for a correct interpretation by the inflater and depends on the type of project in which you are adding attributes. If it is an application, replace com.mydomain.mypackage with the actual package name of your application (application name excluded). If it is a library you must use "http://schemas.android.com/apk/res-auto" (and be using Tools R17 or later) or you will get runtime errors.

有两点要注意:

  • 看来你并不需要调用refreshDrawableState功能,至少该解决方案的工作原理以及是,在我的情况

  • It seems you don't need to call the "refreshDrawableState" function, at least the solution works well as is, in my case

为了使用您的自定义类在布局XML文件,则必须指定完全限定域名(如com.mydomain.mypackage.FoodButton)

In order to use your custom class in a layout xml file, you will have to specify the fully qualified name (e.g. com.mydomain.mypackage.FoodButton)

可以作为WEEL混淆标准规定(如机器人:pressed,机器人:启用,机器人:选择)自定义状态,以便重新present更复杂的状态组合。

You can as weel mix-up standard states (e.g. android:pressed, android:enabled, android:selected) with custom states, in order to represent more complicated state combinations

这篇关于Android的:如何添加自定义按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:39