以编程方式更改android布局 [英] Change android layout programmatically

查看:49
本文介绍了以编程方式更改android布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的布局 main.xml 启动我的应用程序:

I start my application with a layout main.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relative" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ImageButton android:id="@+id/applogo" ... android:src="@drawable/app_logo"/>
    <TabHost android:id="@android:id/tabhost" ... android:layout_below="@+id/applogo">
        <LinearLayout ...>
            <TabWidget.../>
            <FrameLayout android:id="@android:id/tabcontent"...>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

设置菜单中的用户可以选择另一个布局,较小的布局( tiny.xml ),其布局是这样的:

And the user in the settings menu can choose another layout, smaller (tiny.xml), whose layout is this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relative" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TabHost android:id="@android:id/tabhost" ...>
        <LinearLayout ...>
            <TabWidget.../>
            <FrameLayout android:id="@android:id/tabcontent"...>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

mainActivity 扩展了 TabActivity 并在 onCreate 方法中

...

if (isTiny())
setContentView(R.layout.tiny);
else
setContentView(R.layout.main);
mTabHost = getTabHost();

TabSpec newsTab = mTabHost.newTabSpec(NewsActivity.static_getTabActivityTag());
newsIntent = new Intent(this, NewsActivity.class);      
newsTab.setContent(newsIntent);
TextView textView = new TextView(this);

textView.setText(getResources().getString(NewsActivity.static_getIndicatorStringID());
textView.setTextSize(size);
textView.setTextColor(color);
textView.setBackgroundDrawable(background);
tab.setIndicator(textView);
mTabHost.addTab(newsTab);

想法是在 mainActivity#onRestart 中编写一些代码,因此,如果用户通过设置面板更改了布局,则会为其加载新的布局.如何实现呢?我尝试使用 setContentView ,但是它只是崩溃,并再次为选项卡创建视图,但是它不起作用,视图为空白.

The idea is to write some code in mainActivity#onRestart so if the user changed the layout through the settings panel load the new layout for him. How to achieve it? I tried using setContentView but it just crashes and creating the views for the tabs again but it just do not work, the views are blank.

已更新:添加了如何在活动"中创建标签的方法.

Updated: added how to create a tab in the Activity.

更新可以多次执行setContentView.我的问题与Intent中的活动有关.

Update It is possible to do more than once setContentView. My problem was related to the activities in the Intent.

推荐答案

您可以随时调用setContentView,而不必将其放在onCreate中.我会使用 SharedPreferences 来存储用户想要的内容视图.希望这会有所帮助.

You can call setContentView at any time it doesn't have to be in onCreate. I would used aSharedPreferences to store which content view the user wants. Hope this helps.

这篇关于以编程方式更改android布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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