Android 向主题添加不同样式的视图,并在运行时对其进行更改?

Android 向主题添加不同样式的视图,并在运行时对其进行更改?,android,themes,styles,runtime,Android,Themes,Styles,Runtime,我用样式设计了特定的文本视图和其他东西。现在我想实现另一个主题,它应该特别更改styles.xml中定义的颜色。那么,是否可以将所有这些样式添加到一个主题中,并在运行时使用act.setThemethemeID;对其进行更改 以下是一些代码示例: ListView Layout.xml看起来有点混乱,但是样式属性应该很容易找到 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://s

我用样式设计了特定的文本视图和其他东西。现在我想实现另一个主题,它应该特别更改styles.xml中定义的颜色。那么,是否可以将所有这些样式添加到一个主题中,并在运行时使用act.setThemethemeID;对其进行更改

以下是一些代码示例:

ListView Layout.xml看起来有点混乱,但是样式属性应该很容易找到

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

            style="@style/OrangeTheme_ListView_Item"

            android:id="@+id/MainListItemBG" android:padding="10dp"
            android:layout_height="fill_parent"
            android:orientation="vertical" 
            android:layout_width="fill_parent">
    <TextView 
        style="@style/OrangeTheme_ListView_Item_TextHead"

                           android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/MainList_Head" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <TableLayout  android:stretchColumns ="*" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableLayout1">
        <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">

                <TextView 
                    style="@style/OrangeTheme_ListView_Item_TextBody" 
                                       android:layout_width="fill_parent" android:text="test"       
                                       android:layout_height="fill_parent" android:id="@+id/MainList_Body_Left"></TextView>

                <TextView 
                   style="@style/OrangeTheme_ListView_Item_TextBody" android:layout_width="fill_parent" android:text="test"  android:layout_height="fill_parent" android:id="@+id/MainList_Body_Right"></TextView>

            </TableRow>



    </TableLayout>

</LinearLayout>
my styles.xml的一部分

    <style name="OrangeTheme_ListView">
        <item name="android:background">#FFFFFF</item>
    <item name="android:layout_marginRight">5dp</item>
        <item name="android:layout_marginLeft">5dp</item>
    <item name="android:divider">#FFFFFF</item>
        <item name="android:dividerHeight">5dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:cacheColorHint">#00000000</item>          
    </style>


    <style name="OrangeTheme_ListView_Item">
          <item name="android:background">@drawable/orange_theme_lv_bg</item>
          <item name="android:textColor">#000000</item>
          <item name="android:layout_marginRight">10dp</item>
          <item name="android:layout_marginLeft">10dp</item>
          <item name="android:layout_marginBottom">10dp</item>
          <item name="android:layout_marginTop">10dp</item>
    </style>

    <style name="OrangeTheme_ListView_Item_TextHead">
        <item name="android:textColor">#000000</item>
    </style>   

    <style name="OrangeTheme_ListView_Item_TextBody">
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">12sp</item>
    </style>

不幸的是,您还不能在运行时更改样式。 这就是原因。你可以做的是创建不同的布局,并根据你的应用所处的情况加载它们