2

我目前有一个非常具体的挑战。 这是事情:自定义属性 - 方向更改

我建立了一个自定义视图,我想从xml布局中设置一个特定的属性。 我在视图的构造函数中获得我的自定义属性。 现在经过方向更改此构造函数是不再调用,但我为自定义属性设置了不同的值。 是否有任何方式自动获得这些新值,或者我必须手动设置方向更改(或再次膨胀)?

自定义视图:

public MyCustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    TypedArray a = context.getTheme().obtainStyledAttributes(
     attrs, 
     R.styleable.MyCustomView, 
     0, 0); 

    try { 
     mAttr = a.getBoolean(R.styleable.MyCustomView_customAttr, false); 
    } finally { 
     a.recycle(); 
    } 
} 

布局景观

<com.me.MyCustomView 
    custom:customAttr="false" /> 

布局画像

<com.me.MyCustomView 
    custom:customAttr="true" /> 

感谢您的咨询!

回答

1

您是否在使用android:configChanges="orientation"您的活动明显?

这样的:

<activity android:name=".MyActivity" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="@string/app_name"> 

如果你这样做,你所要求的,管理自己的方向变化,所以你有责任为重建所需的视图(再膨胀,就是这样)。您可以在致电活动中完成。

但是,如果你删除此设置,活动默认情况下应重建的话,你的布局将再次与新的属性建立...

如本主题解释:oncreate called on every re-orientation

更多信息约Handling Runtime changes here

+0

非常感谢!解决了!我认为可能会有重新创建视图或手动设置属性的方法...但似乎没有。 – saberrider