如何将样式应用于Android中的嵌套视图
将xml布局文件与以下部分一起使用:如何将样式应用于Android中的嵌套视图,android,layout,styles,textview,selector,Android,Layout,Styles,Textview,Selector,将xml布局文件与以下部分一起使用: <RelativeLayout android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/relativeLayout" android:background="@drawable/custom_drawable" > <TextView
<RelativeLayout
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/relativeLayout"
android:background="@drawable/custom_drawable" >
<TextView
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerVertical="false"
android:layout_centerInParent="true" />
<ImageView
android:src="@drawable/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/imageView11"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
</RelativeLayout>
考虑到“custom_drawable”是不同状态(按下、选中等)的选择器,我想知道是否可以使用“custom_drawable”将样式应用于嵌套元素。例如,根据RelativeLayout的状态更改TextView的颜色
有什么想法吗
编辑:我找到了一种只使用XML文件的方法,请检查我的答案。试试这种方法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rdbOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/radio_selector_background"
android:button="@null"
android:checked="true"
android:padding="2dp"
android:gravity="center"
android:drawableBottom="@drawable/ic_launcher"
android:text="One"
android:textColor="@drawable/radio_selector_color" />
<RadioButton
android:id="@+id/rdbTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/radio_selector_background"
android:button="@null"
android:padding="2dp"
android:gravity="center"
android:drawableBottom="@drawable/ic_launcher"
android:text="Two"
android:textColor="@drawable/radio_selector_color" />
<RadioButton
android:id="@+id/rdbThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/radio_selector_background"
android:button="@null"
android:padding="2dp"
android:gravity="center"
android:drawableBottom="@drawable/ic_launcher"
android:text="Three"
android:textColor="@drawable/radio_selector_color" />
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/orange" android:state_checked="true"></item>
<item android:drawable="@color/white"></item>
</selector>
b。radio_selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true"></item>
<item android:color="@color/orange" ></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#f04e23</color>
<color name="white">#ffffff</color>
</resources>
#f04e23
#ffffff
当我发布时,它会再次改变颜色。我终于找到了一个解决方法,使它只处理XML文件。它包括在嵌套的子视图中使用子句
android:duplicateParentState=“true”
,这样状态从父视图继承
还需要创建一个新的选择器来控制文本颜色/类型等,但这非常简单
使用初始代码的一个示例:
<RelativeLayout
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/relativeLayout"
android:background="@drawable/custom_drawable" >
<TextView
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerVertical="false"
android:layout_centerInParent="true"
android:textColor="@drawable/custom_drawable_text"
android:duplicateParentState="true" />
<ImageView
android:src="@drawable/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/imageView11"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
</RelativeLayout>
您可以执行任何操作,如更改颜色、更改文本外观和隐藏其他视图。你想做的任何事情。是的,这是我已经知道的方式,但我想不用代码,只使用XML文件。。。谢谢。哎呀。。!!我将研究如何使用xml来实现它。这个想法很好,但问题是,在我的例子中,我想修改嵌套元素的样式,而您只是修改同一元素的两个不同属性。
<RelativeLayout
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/relativeLayout"
android:background="@drawable/custom_drawable" >
<TextView
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerVertical="false"
android:layout_centerInParent="true"
android:textColor="@drawable/custom_drawable_text"
android:duplicateParentState="true" />
<ImageView
android:src="@drawable/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/imageView11"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
</RelativeLayout>