java - fragment 选项卡主机有问题 - 它不起作用

标签 java android xml fragment-tab-host

我想做的是在我的主 xml 文件中创建三个选项卡。然后根据已按下的选项卡加载 fragment 。

我在使用 Fragment Tab Host 时遇到问题。我有这个 XML,其中有 ;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#369742"
android:gravity="start|end"
android:longClickable="true">

<FragmentTabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:clickable="false">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:clickable="false"
        android:background="#5DAD68">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:clickable="false"
        android:background="#5DAD68"
        android:weightSum="1">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            android:background="#5DAD68"
            android:clickable="false"
            android:measureWithLargestChild="true"
            android:orientation="horizontal"
            android:layout_weight="1.04"
            android:longClickable="false"
            android:paddingTop="10dp"
            android:paddingLeft="30dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"></TabWidget>

        <ImageButton
            android:layout_width="62dp"
            android:layout_height="40dp"
            android:id="@+id/horsePointer"
            android:backgroundTintMode="src_atop"
            android:src="@drawable/ic_horse"

            android:layout_toRightOf="@android:id/tabs"
            android:background="#5DAD68"
            android:layout_gravity="center_vertical" />
    </LinearLayout>
        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="false">

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:clickable="false"
                android:background="#FFFFFF"
                android:weightSum="1">


            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:clickable="true"
                android:weightSum="1"
                android:background="#FFFFFF"
                android:layout_gravity="bottom">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:clickable="false"
                android:background="#FFFFFF">

            </LinearLayout>

        </FrameLayout>

    </LinearLayout>
</FragmentTabHost>

在我的主要 Activity 中,我得到了这段代码;

public class MainActivity extends ActionBarActivity {

private FragmentTabHost tabHost;
private TextView click;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_tab_main);


    // Refactor at your will.
    tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

    tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("", getResources().getDrawable(R.drawable.ic_tab1)),
            tab1Fragment.class, null);

    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("", getResources().getDrawable(R.drawable.tab2)),
            tab2Fragment.class, null);

    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("", getResources().getDrawable(R.drawable.ic_tab3)),
            tab3Fragment.class, null);
}

当我运行这个程序时,它说“不幸的是,示例项目已停止。”

这是我得到的 Logcat; Logcat

最佳答案

您在 XML 布局中使用了 TabHost,在 Activity 代码中使用了 FragmentTabHost。这两个类是不同的,您一次只能使用其中一个,不能一起使用。

您的 XML 布局需要这样定义:

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

</android.support.v4.app.FragmentTabHost>

关于java - fragment 选项卡主机有问题 - 它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28745903/

相关文章:

java - 服务提供者设计模式

java - 无法找到javac

java - 在 Java 中,通过 TCP 套接字从 C# 应用程序接收音频数据流并在接收时播放流媒体

java - 在 Android 中解析多个段落标签中的文本

xml - django-discover-runner 和 XML 报告?

java - org.springframework.mail.MailSendException : Failed messages: java. lang.NullPointerException

Android 相机预览尺寸问题

android - 无法将 Uri 的 ArrayList 保存在文件 : NotSerializableException

ios - Xcode Storyboard XML 标签含义

不同元素中相同属性的 XML 值不能重复