如何从一个视图导航到另一个视图,如标签栏内的iPhone - Android应用程序

时间:2012-02-14 08:36:18

标签: android tabs

我是android开发的新手。有人可以帮助如何从一个视图导航到标签栏内的另一个视图,如iPhone for Android应用程序吗?

谢谢。

4 个答案:

答案 0 :(得分:1)

检查android中的viewswitcher - 基本前提是您将标记放在xml文件中的2个不同布局中,然后在主活动中声明一个变量ViewSwitcher。然后,要在视图之间切换,只需调用switcher.showNext()和switcher.showPrevious()。

示例: -

private ViewSwitcher switcher;
private static final int REFRESH_SCREEN = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
startScan();
}
      ... Finish the rest of your program

这就是你在视图之间切换的方式

switcher.showNext();  // Switches to the next view
switcher.showPrevious();  // Switches to the previous view

您的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" />
<TextView
    android:text="Loading…"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@+id/progressbar"
    android:gravity="center"/>
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
    android:text="Finished!"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_centerVertical="true" />
</RelativeLayout>

</ViewSwitcher>

答案 1 :(得分:0)

您应该在XML文件中使用Frame Layout来实现此目的。它可以实现为:

android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
>

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

<FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

   <AnalogClock
        android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="A Semi-random Button"             

         />

<Button 
    android:id="@+id/tab2"
    android:text="@string/goToClock"
    android:layout_width="fill_parent"
android:layout_height="wrap_content"


    />

</FrameLayout>

</LinearLayout>

答案 2 :(得分:0)

你的意思是你想使用TabView,我在这里给出了一个样本:

public class Welcome extends TabActivity{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
       // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    intent = new Intent(Welcome.this, Select.class);
    spec = tabHost.newTabSpec("Show All").setIndicator("Show All",getResources().getDrawable(R.drawable.showall)).setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent(Welcome.this, Insert.class);
    spec = tabHost.newTabSpec("Insert").setIndicator("Insert",getResources().getDrawable(R.drawable.insertrelative)).setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent(Welcome.this, Shownetwork.class);
    spec = tabHost.newTabSpec("Show Network").setIndicator("Show Network",getResources().getDrawable(R.drawable.showrelativeinfo)).setContent(intent);
    tabHost.addTab(spec);



 // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent(Welcome.this, Addnetwork.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Add Network").setIndicator("Add Network",getResources().getDrawable(R.drawable.addrelativeinfo)).setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);

这是Welcome.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <FrameLayout  android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" android:background="@drawable/images"/>
</LinearLayout>

答案 3 :(得分:0)

创建一个处理所有子选项卡的选项卡活动,为每个选项卡创建一个活动。并在我们之前创建的主选项卡中将意图传递给该活动。您还可以为该选项卡指定图标图像和名称。

public class TabActivity extends android.app.TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Resusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab

intent = new Intent()。setClass(this,StockList.class);

    spec = tabHost
            .newTabSpec("stocks")
            .setIndicator("Stocks",
                    res.getDrawable(R.drawable.stocklist_tab))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, AddStock.class);
    spec = tabHost
            .newTabSpec("Add stock")
            .setIndicator("Add Stock",
                    res.getDrawable(R.drawable.stockchange_tab))
            .setContent(intent);
    tabHost.addTab(spec);