0

我开发了一个简单的 Android 应用程序,其中我使用 Fragments 实现了选项卡...但我无法显示选项卡的图像..我附上了下面的源代码..请让我知道错误是什么.. .

我的活动类 TabDemoFragmentActivity.java

    public class TabDemoFragmentActivity extends FragmentActivity {

    /**
     * Instance variable of type {@link FragmentTabHost} 
     */
    private FragmentTabHost fragmentTabHost;

    /**
     * This is a call back method, which gets invoked 
     * when the instance of this class is created. 
     * 
     * <p>  
     *      This method is used to set the tab host and 
     *      add the tab host fragments to the screen, 
     *      which acts as a UI.
     * </P>
     */
    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);

        setContentView(R.layout.activity_fragment_main);

        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("DropCall Details").setIndicator("DropCall Details",
                getResources().getDrawable(R.drawable.drop_call_image)).setContent(intent), QoSDropCallDetailsFragment.class, null);

        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Network Details").setIndicator("Network Details",
                getResources().getDrawable(R.drawable.network_details)), QoSNetworkDetailsFragment.class, null);
    }
}

我的 XML 文件

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

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

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

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" />
    </LinearLayout>

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

此处未显示图像..请让我知道如何在选项卡中显示图像..谢谢

4

1 回答 1

0

我意识到这是一个老问题,但希望有人能登陆这个页面。首先,让我指出 FragmentTabHost 是在您的应用程序中添加选项卡的不推荐使用的方法。Google 希望您移至 ActionTabBar 以添加标签。

我做了一个与你非常相似的实现,我看不出有太大的不同。但是,我可以从您的代码中指出的两件事是,首先,您需要确保将图像复制到所有 3 个可绘制文件夹。其次,删除您在 xml 中的额外 Framelayout(tabcontent),因为我只有 1 个 Framelayout,而且我没有看到您在 Activity 类中使​​用另一个 Framelayout。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

    <FrameLayout
        android:id="@+id/tabFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

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

这是我的活动课程的片段:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fragment);
        mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
                        getResources().getDrawable(R.drawable.user_card)),
                TabActivity.ListFragment.class, null);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
                        getResources().getDrawable(R.drawable.menu)),
                        TabActivity.ListFragments.class, null);
}
于 2014-03-31T21:30:49.797 回答