我必须向RelativeLayout 添加新状态,但永远不会调用onCreateDrawableState 方法。
我的班级是:
public class UnreadableRelativeLayout extends RelativeLayout
{
private static final int[] STATE_UNREADED = {R.attr.state_unreaded};
private boolean isUnreaded = false;
public UnreadableRelativeLayout(Context context)
{
super(context);
}
public UnreadableRelativeLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public UnreadableRelativeLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void setUnreaded(boolean isUnreaded)
{
if (this.isUnreaded != isUnreaded)
{
this.isUnreaded = isUnreaded;
refreshDrawableState();
}
}
@Override
protected int[] onCreateDrawableState(int extraSpace)
{
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isUnreaded) mergeDrawableStates(drawableState, STATE_UNREADED);
return drawableState;
}
}
为什么 onCreateDrawableState 方法永远不会调用?
尝试创建单独的布局android:duplicateParentState="true"
并在你的课堂上给它充气。它适用于 Android 4.0.4:
不可读的相对布局.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:duplicateParentState="true" >
...
</RelativeLayout>
不可读的相对布局.java:
public class UnreadableRelativeLayout extends RelativeLayout {
public UnreadableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater factory = LayoutInflater.from(context);
factory.inflate(R.layout.unreadable_relative_layout, this);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)