谁能提供 Android 中 TabHost 的示例代码?

发布于 2024-07-23 10:04:26 字数 44 浏览 3 评论 0原文

我需要示例代码来在 android 中创建 TabHost。 谁能帮我。

I need sample code to create TabHost in android.
can anyone help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东京女 2024-07-30 10:04:26

Android 开发者网站有一个出色的完整代码示例,用于使用 TabWidgetTabHost 在 Android 中创建选项卡。

查看你好,TabWidget

The Android Developer site has an excellent fully worked code sample for creating tabs in Android using the TabWidget and TabHost.

Check out Hello, TabWidget.

财迷小姐 2024-07-30 10:04:26

我已经完成了与显示分数相关的 tabhost 代码

    TabHost host = getTabHost();
    host.setup ();

    TabSpec allScoresTab = host.newTabSpec("allTab");
    allScoresTab.setIndicator(getResources().getString(R.string.all_scores), getResources().getDrawable(android.R.drawable.star_on));
    allScoresTab.setContent(R.id.ScrollViewAllScores);
    host.addTab(allScoresTab);

    TabSpec friendScoresTab = host.newTabSpec("friendsTab");
    friendScoresTab.setIndicator(getResources().getString(R.string.friends_scores), getResources().getDrawable(android.R.drawable.star_on));
    friendScoresTab.setContent(R.id.ScrollViewFriendScores);
    host.addTab(friendScoresTab);

    host.setCurrentTabByTag("allTab");

我的 xml 包含:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bkgrnd">
    <RelativeLayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ImageView_Header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quizicon"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">
        </ImageView>
        <TextView
            android:id="@+id/TextView01"
            android:layout_height="wrap_content"
            android:text="@string/scores"
            android:textSize="@dimen/screen_title_size"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:layout_width="wrap_content"
            android:layout_gravity="fill_horizontal|center"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:shadowColor="@android:color/white"
            android:textColor="@color/title_color">
        </TextView>
        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ImageView_Header2"
            android:layout_height="wrap_content"
            android:src="@drawable/quizicon"
            android:layout_gravity="right|center_vertical"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true">
        </ImageView>
    </RelativeLayout>
   <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">
        <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">
            <ScrollView android:id="@+id/ScrollViewAllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
  <TableLayout android:id="@+id/TableLayout_AllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" /> 
  </ScrollView>
- <ScrollView android:id="@+id/ScrollViewFriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
  <TableLayout android:id="@+id/TableLayout_FriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" /> 
  </ScrollView>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

I had done tabhost related code with respect to displaying scores

    TabHost host = getTabHost();
    host.setup ();

    TabSpec allScoresTab = host.newTabSpec("allTab");
    allScoresTab.setIndicator(getResources().getString(R.string.all_scores), getResources().getDrawable(android.R.drawable.star_on));
    allScoresTab.setContent(R.id.ScrollViewAllScores);
    host.addTab(allScoresTab);

    TabSpec friendScoresTab = host.newTabSpec("friendsTab");
    friendScoresTab.setIndicator(getResources().getString(R.string.friends_scores), getResources().getDrawable(android.R.drawable.star_on));
    friendScoresTab.setContent(R.id.ScrollViewFriendScores);
    host.addTab(friendScoresTab);

    host.setCurrentTabByTag("allTab");

My xml contains :

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bkgrnd">
    <RelativeLayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ImageView_Header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quizicon"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">
        </ImageView>
        <TextView
            android:id="@+id/TextView01"
            android:layout_height="wrap_content"
            android:text="@string/scores"
            android:textSize="@dimen/screen_title_size"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:layout_width="wrap_content"
            android:layout_gravity="fill_horizontal|center"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:shadowColor="@android:color/white"
            android:textColor="@color/title_color">
        </TextView>
        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ImageView_Header2"
            android:layout_height="wrap_content"
            android:src="@drawable/quizicon"
            android:layout_gravity="right|center_vertical"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true">
        </ImageView>
    </RelativeLayout>
   <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">
        <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">
            <ScrollView android:id="@+id/ScrollViewAllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
  <TableLayout android:id="@+id/TableLayout_AllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" /> 
  </ScrollView>
- <ScrollView android:id="@+id/ScrollViewFriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
  <TableLayout android:id="@+id/TableLayout_FriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" /> 
  </ScrollView>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>
南风几经秋 2024-07-30 10:04:26

Activity_main.xml 包含

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/scroll" />

        <HorizontalScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:layout_alignParentBottom="true" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/tabs_bg" />
        </HorizontalScrollView>
    </RelativeLayout>

</TabHost>

java 代码是

TabHost tabHost = getTabHost();

        TabSpec spec;

        Intent intent;

        //Home Tab
        View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);

        intent = new Intent(MainActivity.this, Firstclass.class);

        spec = tabHost.newTabSpec("HOME").setIndicator(view1)
                .setContent(intent);

        tabHost.addTab(spec);

        //Calendar Tab
        View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);

        intent = new Intent(MainActivity.this, Calendar.class);

        spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
                .setContent(intent);

        tabHost.addTab(spec);

activity_main.xml contains

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/scroll" />

        <HorizontalScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:layout_alignParentBottom="true" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/tabs_bg" />
        </HorizontalScrollView>
    </RelativeLayout>

</TabHost>

and java code is

TabHost tabHost = getTabHost();

        TabSpec spec;

        Intent intent;

        //Home Tab
        View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);

        intent = new Intent(MainActivity.this, Firstclass.class);

        spec = tabHost.newTabSpec("HOME").setIndicator(view1)
                .setContent(intent);

        tabHost.addTab(spec);

        //Calendar Tab
        View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);

        intent = new Intent(MainActivity.this, Calendar.class);

        spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
                .setContent(intent);

        tabHost.addTab(spec);
做个少女永远怀春 2024-07-30 10:04:26

资源/布局/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

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

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

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffc916"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 1" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#da8200"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 2" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#5b89ff"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

src/MainActivity.java

  public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost host = (TabHost)findViewById(R.id.tabHost);
    host.setup();

    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("Tab One");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Tab One");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("Tab Two");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Tab Two");
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("Tab Three");
    spec.setContent(R.id.tab3);
    spec.setIndicator("Tab Three");
    host.addTab(spec);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
} 

您可以按照这些教程获取帮助

使用 TabHost 创建选项卡式 UI

Android 选项卡布局教程

res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

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

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

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffc916"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 1" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#da8200"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 2" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#5b89ff"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is tab 3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

src/MainActivity.java

  public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost host = (TabHost)findViewById(R.id.tabHost);
    host.setup();

    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("Tab One");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Tab One");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("Tab Two");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Tab Two");
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("Tab Three");
    spec.setContent(R.id.tab3);
    spec.setIndicator("Tab Three");
    host.addTab(spec);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
} 

You can Follow these tutorials for help

Creating a tabbed UI with TabHost

Android Tab Layout Tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文