如何在复合自定义视图上设置主题

时间:2015-11-05 23:33:35

标签: android

我试图在Page2属性和一些自定义视图属性的帮助下,在由其他视图组成的自定义视图上设置主题。 代码看起来像这样:

IconTextView.java

theme

attrs_icon_text_view.xml

public class IconTextView extends LinearLayout {

    public IconTextView(Context context) {
        super(context);
        init(context, 0);
    }

    public IconTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, 0);
    }

    public IconTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, defStyleAttr);
    }

    private void init(Context context, int defStyleAttr) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_icon_text, this, true);
    }
}

view_icon_text.xml

<declare-styleable name="IconTextViewTheme">
    <attr name="textViewStyle" format="reference" />
    <attr name="iconStyle" format="reference" />
</declare-styleable>

styles.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        style="?iconStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@android:drawable/ic_menu_share"
        android:contentDescription="@null" />

    <TextView
        style="?textViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:text="Hello"/>
</LinearLayout>

layout_main.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">      
    </style>

    <style name="Custom.IconTextView" parent="AppTheme">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:textSize">30sp</item>
        <item name="android:padding">8dp</item>
    </style>

    <style name="Custom.Test" parent="AppTheme">
        <item name="textViewStyle">@style/Special.IconTextView</item>
    </style>
</resources>

不幸的是,如果通过预览的主题选择器选择主题,这仅适用于Android Studio预览。它不适用于<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <IconTextView app:theme="@style/Custom.Test" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 属性。

theme属性是否应该将主题中包含的样式应用于视图及其子项? 这甚至是可能的还是我应该将主题应用于整个活动?

2 个答案:

答案 0 :(得分:0)

我认为你想要实现的目标在这里用几个备选方案来描述

https://gyula-juhasz.com/blog/styling-composite-views-in-android-part-1/

答案 1 :(得分:0)

对于像我这样想知道该问题可能解决方案的人。 您可以执行以下操作:

attrs.xml

<!-- Define your attribute that you can use in a main theme -->
<attr name="iconTextViewTheme" format="reference"/>

<declare-styleable name="IconTextViewTheme">
    <attr name="textViewStyle" format="reference" />
    <attr name="iconStyle" format="reference" />
</declare-styleable>

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
        <!-- Define what style you want to use for this component in AppTheme -->
        <!-- If you have multiple themes, you can customize this per theme -->    
        <item name="iconTextViewTheme">@style/myCustomIconTextViewTheme</item>
    </style>

    <style name="myCustomIconTextViewTheme">
        <item name="textViewStyle">@style/insertYourCustomTextViewStyleHere</item>
        <item name="iconStyle">@style/insertYourCustomImageViewStyleHere</item>
    </style>
</resources>

view_icon_text.xml

<!-- Apply the style with android:theme using an attribute applies the style to child views -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="?attr/iconTextViewTheme">

    <ImageView
        style="?attr/iconStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@android:drawable/ic_menu_share"
        android:contentDescription="@null" />

    <TextView
        style="?attr/textViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:text="Hello"/>
</LinearLayout>