0

I'm trying to enable users to change layout of my apps.

This is my XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menuTheme"
    android:orderInCategory="1"
    android:showAsAction="never"
    android:title="Theme">
    <menu>
        <item
            android:id="@+id/themeBlack"
            android:title="Black" />
        <item
            android:id="@+id/themeWhite"
            android:title="White" />
    </menu>
</item>

<item
    android:id="@+id/menuAbout"
    android:orderInCategory="1"
    android:showAsAction="never"
    android:title="About" />

Here are the screenshots from my emulator: This is my menu.

This is my menu.

After clicking the item Theme under the menu, this submenu will appear. After clicking the item Theme under the menu, this submenu will appear

Lets say i wish to add a white layout, where and how should i add codes?I have a XML file named themewhite.XML in the res/layout folder. I tried to add a switch in the item theme but it doesn't seemed to be correct.

This is my code:

    @Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.menuTheme:
        {
            switch(item.getItemId())
            {
                case R.id.themeWhite;
                {
                    setContentView(R.layout.themewhite);
                }
            }
            break;
        }
        case R.id.menuAbout:
        {
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}
4

1 回答 1

1

我猜你不想设置新的布局,你只想改变现有的可视化。

如果是这样,您应该使用您的活动主题。首先,您需要使用样式中的值创建主题(白色等)和贴花视图属性。然后在你的听众中点击其中一项后,你应该通过调用来改变你的主题
getContext().setTheme(resid)

这里有一篇关于主题的好文章http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

于 2013-07-16T13:45:40.373 回答