Fragment 之间的 Android Tab 导航

标签 android android-fragments tabs navigation

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

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

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

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

提前致谢。

最佳答案

您需要创建一个 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 文件夹中。

Activity 的编码应类似于以下代码:

公共(public)类 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 和类):

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>

关于Fragment 之间的 Android Tab 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25103557/

相关文章:

android - Android 中 fragment 的深层层次结构

java - Android 中的 "Activity not found"

java - Android TabWidget 空白问题

android - HTML 输入字段 : Automatically display numeric input method

android - Libgdx:Touch up 事件意外发生

android - 在两项 Activity 之间来回切换的最佳方式?

android - 如果在任何 Activity 中打开键盘,Tabgroupactivity 的 OnBackpressed 将不起作用

javascript - 在条形图和折线图上具有双轴的 Chart.js

android - 如何在 Android 中保护 Assets 文件

android - ActionBarCompat 微调器不工作