安卓:传播父状态子视图 [英] Android: Propagate parent state to child view
问题描述
我遇到了一些问题,我根本不知道如何解决。我有一些孩子compenents在里面,像ImageView的,TextView的等等,一个例子可以在下面的XML中可见的LinearLayout:
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 be seen in the xml below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/list_buttom_top">
<LinearLayout android:gravity="center_vertical"
android:paddingLeft="10dip"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView android:text="title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
style="@style/content_page_large_text" />
<TextView android:text="summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subtitle"
android:visibility="visible"
style="@style/content_page_small_text" />
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemCount"
android:visibility="gone"
style="@style/content_page_large_count_text" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chevron"
style="@style/list_buttom_chevron" />
</LinearLayout>
风格list_buttom_top说,当年的LinearLayout是在pressed状态下,背景颜色需要改变,到目前为止,这是工作。问题是,content_page_large_text还表示,需要的TextView的文本颜色被改变为好,但它不发生的。当我点击的LinearLayout唯一的颜色变化是它的背景。
The style list_buttom_top says that when the LinearLayout is in the pressed state, the background color needs to be changed, and so far this is working. The problem is that the content_page_large_text also says that the text color of the TextView needs to be changed as well, but it doesnt happen. The only color changing when I am clicking the LinearLayout is its background.
<style name="list_buttom_top">
<item name="android:clickable">true</item>
<item name="android:paddingTop">10dip</item>
<item name="android:paddingBottom">10dip</item>
<item name="android:paddingLeft">10dip</item>
<item name="android:paddingRight">10dip</item>
<item name="android:gravity">center_vertical</item>
<item name="android:background">@drawable/background_view_rounded_top</item>
</style>
<style name="list_buttom_chevron">
<item name="android:paddingLeft">4.0dip</item>
<item name="android:paddingTop">2.0dip</item>
<item name="android:paddingRight">4.0dip</item>
<item name="android:paddingBottom">2.0dip</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">@drawable/chevron</item>
</style>
我用这个机器人曾尝试:在我的TextViews duplicateParentState =真正的,但它也没有工作。同样的情况在ImageView的,这不,当我点击的LinearLayout获得其源图像改变。
I have tried using this android:duplicateParentState="true" in my TextViews but it also didnt work. The same happens for the ImageView, which does not get its source image changed when I am clicking the LinearLayout.
我失去了一些东西呢? 感谢您的关注 牛逼
Am I missing something here? Thanks for the attention T
推荐答案
这将是一个更简单的问题跟踪,如果我们能看到你的content_page_large_text风格。另外,我看不出你如何在实际设置的颜色,是你的background_view_rounded_top一个&LT;选择&GT;
元素,或者是你在活动设置在一个监听器?
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?
不过,是的,我最近还发现,安卓duplicateParentState =true是不是,我认为它是万能的。
But yeah I also recently discovered that android:duplicateParentState="true" isn't the panacea that I thought it was.
如果这两种解决方案被耗尽,然后我去到解决方案通常是根布局的自定义组件。
http://developer.android.com/guide/topics/ui/custom -components.html
但是,这可能不是必要的,如果接口是这个简单。我是pretty的保证您可以用 StateListDrawable
取值做到这一点。
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 StateListDrawable
s.
自定义组件通常比试图找到XML变量的神奇组合更容易,并让布局更容易理解/编辑以后,如果你打算让这更复杂。
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.
这篇关于安卓:传播父状态子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!