gpt4 book ai didi

android - StateListDrawable 在自定义 View 中不起作用

转载 作者:行者123 更新时间:2023-11-30 03:54:39 26 4
gpt4 key购买 nike

我在自定义 View 上遇到了 StateListDrawable 的大问题。我有一个直接从 LinearLayout 继承的自定义 View ,在它的 XML 布局中,背景是一个简单的状态列表可绘制对象,它按照当前状态的顺序设置 View 的背景启用或禁用。我不明白的是为什么如果我打电话

this.SetEnabled(false);

在我的自定义 View 中,背景没有改变。

也许我的问题不是很清楚,所以我做了一个简单的例子。可以下载here .查看“ViewCustom.java”文件并找到 FIXME 标记。

希望有人能帮助我。

附言与 Button 关联的相同状态列表 drawable 可以工作,但在我的自定义 View 上不行。

ViewCustom.java

public class ViewCustom extends LinearLayout {

public ViewCustom(Context context, AttributeSet attrs) {
super(context, attrs);

//Inflate layout
final LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_custom, this);


/**FIXME: Try to set "enabled" to false in order to get the
* "panel_disabled" background, but it doesn't work.
*/
this.setEnabled(false);
}

查看.custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sld_panel"
android:orientation="vertical" >
</LinearLayout>

sld_面板.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/panel_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/panel_enabled" android:state_enabled="true"/>
</selector>

最佳答案

问题是,您不能只禁用线性布局。您需要做的是禁用其中的所有内容。 This question回答了这个问题,但它归结为两件事之一。

  1. 您可以通过 setVisibility(View.GONE) 使布局消失。
  2. 您可以通过如下方式遍历 View 中的每个项目:

    for ( int i = 0; i < myLayout.getCount();  i++ ){
    View view = getChildAt(i);
    view.setEnabled(false); // Or whatever you want to do with the view.
    }

关于android - StateListDrawable 在自定义 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13542353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com