【问题标题】:How to apply style to the nested views in Android如何将样式应用于 Android 中的嵌套视图
【发布时间】:2014-01-29 10:40:34
【问题描述】:

使用带有如下部分的 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 文件的方法,请检查我的答案。

【问题讨论】:

    标签: android layout styles textview selector


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      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 的颜色 当我释放它时它会再次改变它的颜色。

      【讨论】:

      • 您可以更改颜色、更改文本外观和隐藏其他视图等任何操作。任何你想做的事情。
      • 是的,这是我已经知道的方式,但我想不用代码,只使用 XML 文件......谢谢。
      • 哎呀..!!好吧,我会为它研究如何使用 xml。
      【解决方案3】:

      试试这个方法

      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>
        

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

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

      【讨论】:

      • 这个想法很好,但问题是我想修改嵌套元素的样式,而您只是修改同一元素的两个不同属性。
      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2015-11-17
      • 2012-11-13
      • 2011-06-21
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多