开发者

Using Views in a tabbed application

Alright, so I have an application that has a tabbed interface. What I would like to know is how I can change the currently active View in the tab, and then when I'm done with that View, go back the the original View, e开发者_高级运维xactly as it was.


You may also want to use a ViewFlipper, defined in xml.

<ViewFlipper android:id="@+id/ScreenSwitch"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <element A>
    <element B>
    <element C>
</ViewFlipper>

Usage is simple, just find the ViewFlipper object throught findViewById() and request changing:

viewFlipper.setDisplayedChild( idOfViewToSet );

It's then completely up to you, how do you build "tabs".


For creating the view, what you probably want is something like this:

import android.view.View;
import android.widget.TabHost;

public class MyViewController implements TabHost.TabContentFactory
{
    private View myRootView;

    @Override
    public View createTabContent(String tag)
    {
        if (myRootView == null)
        {
            // TODO: Create your view here by...
            //       1) Constructing it yourself
            //       2) Inflating a layout.xml with LayoutInflater (better)
            myRootView = new View(context);
        }
        return myRootView;
    }
}

As for changing tabs programmatically, it is as simple as calling setCurrentTab() on your TabHost object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜