如何通过单击菜单项来改变布局? [英] How to change layout by clicking items of menu?

查看:191
本文介绍了如何通过单击菜单项来改变布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户更改我的应用程序的布局。

这是我的XML:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android><项目
    机器人:ID =@ + ID / menuTheme
    机器人:orderInCategory =1
    机器人:showAsAction =从不
    机器人:标题=主题>
    <菜单>
        <项目
            机器人:ID =@ + ID / themeBlack
            机器人:标题=黑/>
        <项目
            机器人:ID =@ + ID / themeWhite
            机器人:标题=白/>
    &所述; /菜单>
< /项目><项目
    机器人:ID =@ + ID / menuAbout
    机器人:orderInCategory =1
    机器人:showAsAction =从不
    机器人:标题=关于/>

下面是从我的仿真器的截图:
这是我的菜单。

点击菜单下的项目主题后,该子菜单将出现。

可以说,我想补充一个白色的布局,并在那里我应该如何添加codeS?我在res /布局文件夹中名为themewhite.XML一个XML文件。我试图在该项目的主题添加一个开关,但它不似乎是正确的。

这是我的code:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项)
{
    开关(item.getItemId())
    {
        案例R.id.menuTheme:
        {
            开关(item.getItemId())
            {
                案例R.id.themeWhite;
                {
                    的setContentView(R.layout.themewhite);
                }
            }
            打破;
        }
        案例R.id.menuAbout:
        {
            打破;
        }
    }
    返回super.onOptionsItemSelected(项目);
}


解决方案

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

如果因此你应该与你的活动主题manilpuate。对于首先你需要创建一个主题(白色等),并decalre看法与使用从风格属性值。然后在你的监听器后点击项目之一,你应该通过调用结果更改主题
的getContext()。setTheme(渣油)

下面有关主题的http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

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.

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);
}

解决方案

I guess you don't want to set new layout, you want change just visualisation of existing.

If so you should manilpuate with themes of your activity. For first you need to create themes (white and so on) and decalre views attributes with using values from style. And then in your listener after click one of items, you should change your theme by calling
getContext().setTheme(resid)

Here a good article about themes http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

这篇关于如何通过单击菜单项来改变布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆