Android 自定义视图忽略 XML 中的`android:enabled`? [英] Android custom view ignoring `android:enabled` in XML?

查看:26
本文介绍了Android 自定义视图忽略 XML 中的`android:enabled`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自定义视图上设置 android:enabled="false",但它似乎对 isEnabled() 属性没有任何影响.

I'm setting android:enabled="false" on a custom view, but it doesn't appear to be having any effect on the isEnabled() property.

这是一个简单的测试用例:

Here's a simple test case:

public class TestView extends View {

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setTextAlign(Align.CENTER);
        p.setTextSize(ViewUtils.dpToPx(this, 10));
        canvas.drawText("Enabled = " + (isEnabled() ? "true" : "false"),
                getWidth() / 2,
                getHeight() / 2,
                p);
    }
}

以及相应的 XML:

<com.example.TestView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/darker_gray"
    android:enabled="false"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

输出视图如下所示:

谁能解释一下为什么?

推荐答案

据我所知,android:enabled 不是 View 的属性,因为它不在文档中.

As far as I can tell, android:enabled is not an attribute of View, as it is not in the documentation.

我认为没有办法通过 XML 布局文件修改 isEnabled() 的返回值.(除非为自定义视图定义自定义属性)

I don't think there is a way to modify the return value of isEnabled() via the XML layout file. (Unless defining custom attributes for a custom view)

这篇关于Android 自定义视图忽略 XML 中的`android:enabled`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆