【问题标题】:Android Tab Navigation between Fragments片段之间的Android选项卡导航
【发布时间】:2014-08-03 09:58:35
【问题描述】:

我想知道以下屏幕中的这些导航选项卡是如何实现的:

http://s1.directupload.net/images/user/140803/6pg9mpk7.png

由于这些导航选项卡在手机的多个菜单中使用,它应该是标准的 android 布局项。 他们使用 FragmentTabHost 吗?如果是,您如何设法像这样标记选定的选项卡?我刚刚找到了所选选项卡带有下划线的解决方案。

如果有人可以提供解释或教程链接,那就太好了。

提前致谢。

【问题讨论】:

标签: android android-fragments tabs navigation


【解决方案1】:

你需要创建一个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="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"/>
    </LinearLayout>
</TabHost>

对于每个图标,你需要创建如下的xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/photos_gray"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/photos_white" />
</selector>

drawables 需要在 drawables 文件夹中。

活动的编码应该类似于下面的代码:

公共类 AndroidTabLayoutActivity 扩展 TabActivity {

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

        TabHost tabHost = getTabHost();

        // Tab for Groups
        TabSpec groupSpec = tabHost.newTabSpec("Gruppen");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, GroupActivity.class);
        photospec.setContent(photosIntent);

        // Tab for Telephones
        TabSpec telephoneSpec = tabHost.newTabSpec("Telefon");        
        songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, TelephoneActivity.class);
        songspec.setContent(songsIntent);

        // Tab for Contacts
        TabSpec contactSpec = tabHost.newTabSpec("Kontacte");
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, ContactActivity.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(groupSpec);
        tabHost.addTab(telephoneSpec);
        tabHost.addTab(contactSpec);
    }
}

最后,您需要为每个选项卡创建两个文件(xml 和 class):

public class KontactActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kontacte_layout);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView android:text="Contacts here"
            android:padding="15dip"
            android:textSize="18dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

【讨论】:

  • 感谢您,但不推荐使用 TabActivity。我找到了 FragmentActivities 的解决方案,但这不适用于 Android > 4.0,因为图像不会显示在选项卡上。为了解决这个问题,我使用 Image + Text 制作了一个自定义 Tab.xml,然后显示图像,然后像这个接受的答案建议:stackoverflow.com/questions/10745092/…。但这会导致另一个问题。选择不再起作用。导航有效,但您看不到选择了哪个选项卡。有人对此有解决方案吗?
  • 您尝试过使用 appcompat 吗?
  • 好的,谢谢。由于 Tabhost 已被弃用,我不想使用它,但您的回答仍然引导我朝着正确的方向前进,谢谢。不过,我不能赞成你的回答,因为我还没有 15 个声望点,抱歉。
【解决方案2】:

所以几天前我找到了解决方案。我使用本教程来实现这些类型的选项卡:

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

类似的教程有很多,但是如果您使用的 Holo.Light 主题并不经常被提及,就会出现问题。问题是图像不会显示在选项卡中,您只能看到文本。

要解决这个问题,我必须确保在 style.xml 中包含此主题的 Tab.Widget:

<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item>
</style>

现在,即使我使用 Holo.Light,标签的图像也会显示出来。如果您使用的是 Theme.Black 或 Light,则应该不会出现此问题。

感谢 Andrei Buneyeu 对这个旧问题的回答,我找到了这个解决方案: Icon in Tab is not showing up

【讨论】: