开发者

Change android layout programmatically

开发者 https://www.devze.com 2023-01-30 13:06 出处:网络
I start my application with a layout main.xml like this: <?xml version=\"1.0\" encoding=\"utf-8\"?>

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>

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>

The mainActivity extends TabActivity and in 开发者_开发技巧the onCreate method:

...

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

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.

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


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.


For fragments layout I do it like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (MainActivity.isDarkMode) {
        return inflater.inflate(R.layout.layout_dark, container, false);
    } else {
        return inflater.inflate(R.layout.layout_light, container, false);
    }
}

So you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号