0

我有一个相对布局,它使用状态列表在按下、聚焦和禁用状态下显示不同的可绘制对象。现在按下状态,正常工作正常,但是当我做 a 时relativeLayoutObject.setEnabled(false),不显示正确的可绘制对象。我也有来自 XML 的relativeLayoutObject.setClickable(false)设置android:state_enabled="false",但它们都不起作用。

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:clickable="true"
    android:layout_height="match_parent">

        <!--SOME ITMES INSIDE THE LAYOUT-->
<RelativeLayout>

选择器是

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_d" android:state_enabled="false"/>
    <item android:drawable="@drawable/drawable_n" android:state_focused="true"/>
    <item android:drawable="@drawable/drawable_p" android:state_pressed="true"/>
    <item android:drawable="@drawable/drawable_n"/>
</selector>

我将选择器应用为:relativeLayoutObject.setBackgroundDrawable(R.drawabled.button_state_list);

4

1 回答 1

2

刚刚遇到你的问题,即使你问了 3 年了 :)

RelativeLayouts 没有禁用/启用状态,所以我所做的是最初为我的 RelativeLayout 设置一个背景可绘制样式,如下所示:

        <RelativeLayout
            android:id="@+id/activity_register_btn"
            ...
            android:background="@drawable/style_button_yellow_disabled_rounded">

            <!-- Register button -->
            <TextView
                ... />
        </RelativeLayout>

所以我的风格代码(在drawbles文件夹中):

style_button_yellow_disabled_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- view background color -->
    <solid
        android:color="#ffdca5" >
    </solid>
    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#ffdca5" >
    </stroke>
    <!-- If you want to add some padding -->
    <padding
        android:left="12dp"
        android:top="12dp"
        android:right="12dp"
        android:bottom="12dp">
    </padding>
    <!-- Here is the corner radius -->
    <corners
        android:radius="30dp">
    </corners>
</shape>

然后,在我的 java 文件中,当需要时,我将背景更改为另一个 xml,它是启用的视图:

style_button_yellow_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- view background color -->
    <solid
        android:color="#fcc002" >
    </solid>
    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#fcc002" >
    </stroke>
    <!-- If you want to add some padding -->
    <padding
        android:left="12dp"
        android:top="12dp"
        android:right="12dp"
        android:bottom="12dp">
    </padding>
    <!-- Here is the corner radius -->
    <corners
        android:radius="30dp" >
    </corners>
</shape>
于 2017-02-01T10:56:26.133 回答