如何将样式应用于Android中的嵌套视图

时间:2014-01-29 10:40:34

标签: 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
        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文件的方法,查看我的答案。

3 个答案:

答案 0 :(得分:2)

我终于找到了一种解决方法,使其仅与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>

答案 1 :(得分:0)

试试这种方式

  1. activity_main.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>
    
  2. 在drawable文件夹下定义两个xml文件。(用于颜色和背景选择器)

    一个。 radio_selector_background.xml

    <?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>
    

    湾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>
    
  3. 在values文件夹下定义colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="orange">#f04e23</color>
        <color name="white">#ffffff</color>
    </resources>
    

答案 2 :(得分:0)

btn1.setOnTouchListener(new OnTouchListener() {
 @Override
public boolean onTouch(View v, MotionEvent event) {
 if(event.getAction() == MotionEvent.ACTION_DOWN) {
 btn2.setTextColor(Color.parseColor("#ad321a"));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btn2.setTextColor(Color.parseColor("#Fad213"));
}
return false;
}
});

我已经为这两个按钮编写了这段代码 当我按下按钮然后它将改变btn2的颜色 当我释放然后它再次改变它的颜色。

相关问题