質問

これは、Androidの最初のタブ付きアプリケーションです。 Androids Webサイトで「HelloTabwidget」アプリを歩きましたが、タブにコンテンツを追加する方法を理解できません。 framelayoutは、左上に互いに上に積み重ねられます(私が読んだものから)。いくつかのTextViewsとImageViewを追加しましたが、最後に追加されたアイテムのみを表示します。フレームレイアウトの代わりに線形レイアウトを使用する方法はありますか?そうでない場合、タブに複数のビューを配置するにはどうすればよいですか?例とは異なることをした唯一のことは、4番目のタブを追加したことです。タブアクティビティの1つで、次のコードを挿入して、複数のアイテムを表示しようとします。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        TextView textview = new TextView(this);
        textview.setText("This is the About tab");
        setContentView(textview);

        TextView textview2 = new TextView(this);
        textview2.setText("About Test");
        setContentView(textview2);

        ImageView imgView = new ImageView(this);
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
        setContentView(imgView);
    }

これが私が従った例へのリンクです:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

役に立ちましたか?

解決

レイアウトXMLファイルは何ですか?これを使用し、すべてのタブでいくつかのテキストビューを積み重ねることができます。タバアクティブ:

<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">
    <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" >
        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <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>
    </ScrollView>
</TabHost>

タブ内のアクティビティ:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView android:id="@+id/textOne" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/textTwo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
<LinearLayout>

私は自分自身が初心者なので、それをするためのより良い方法があるかもしれません。しかし、これは私にとってはうまくいきます。

他のヒント

この例を注意深く読みませんでした。ポイント番号6をご覧ください。あなたは次のようなものを見るでしょう:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
}

それはあなたがオンクリートに入れたものです TabActivity. 。ご覧のとおり、3つのアクティビティがあります。あなたがしていることは、1つのアクティビティだけを使用し、コンテンツビューを3回設定することです。これは明らかに間違っています。

だから...それを機能させる方法は?まず、チュートリアルをもう一度お読みください。次に、表示するタブごとに1つのアクティビティを作成します。上記のモデルを使用して、それらのアクティビティをあなたに追加します TabHost.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top