0

Fragments を使用してタブを実装したシンプルな Android アプリケーションを開発しました...しかし、タブの画像を表示できません..以下のソースコードを添付しました..何がエラーなのか教えてください.. .

私のアクティビティクラス 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 に移動してタブを追加することを望んでいます。

私はあなたと非常によく似た実装を行いましたが、大きな違いはありません。ただし、コードから指摘できる2つのことは、最初に、3つの描画可能なフォルダーすべてに画像をコピーすることを確認する必要があることです。次に、xml にある余分な Framelayout(tabcontent) を削除します。これは、Framelayout が 1 つしかなく、Activity クラスで他のフレームレイアウトを使用していないためです。

<?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 に答える