xml属性在xml中不起作用,仅在代码中

时间:2014-08-02 13:00:57

标签: android xml

我使用带有ImageButton的布局。 当我设置属性

android:focusable="false"

在xml文件中按钮仍然可以获得焦点。

当我在代码中执行此操作时:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.planning_line);
    View knop = findViewById(R.id.button_calendar);
    knop.setFocusable(false);

它有效。

在xml文件和代码中设置属性有什么区别?

2 个答案:

答案 0 :(得分:0)

尝试添加第android:focusableInTouchMode="true"

答案 1 :(得分:0)

行为应该是这样,因为'focusable'属性在ImageButton构造函数代码中显式设置为值'true':

    public ImageButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFocusable(true);
}

为什么要这样做的问题仍然存在。