Android: Propagate parent state to child view

后端 未结 2 1006
不思量自难忘°
不思量自难忘° 2021-02-02 13:36

I am experiencing some problem that I basically dont know how to fix. I have a LinearLayout with some child compenents in it, like ImageView, TextView and so on, one example can

相关标签:
2条回答
  • 2021-02-02 14:14

    I think that you should set the android:addStatesFromChildren="true" in the linear layout, this way the children's state is propagated to the parent.

    Also, make sure NOT to combine this with android:duplicateParentState="true" since it will cause a stack overflow exception (basically causing an infinite reference loop).

    0 讨论(0)
  • 2021-02-02 14:15

    It would be an easier issue to track if we could see your content_page_large_text style. Also, I don't see how you're actually setting the color, is your background_view_rounded_top a <selector> element or are you setting it in a listener in the activity?

    But yeah I also recently discovered that android:duplicateParentState="true" isn't the panacea that I thought it was.

    Should those two solutions be exhausted then my go-to solution usually is a custom component for the root layout. http://developer.android.com/guide/topics/ui/custom-components.html But that might not be necessary if the interface is this simple. I'm pretty sure you can do this with StateListDrawables.

    Custom components are usually easier than trying to find the magic combination of xml variables, and will make the layout more understandable/editable later if you're planning on making this more complicated.

    0 讨论(0)
提交回复
热议问题