选项卡下的列表视图

发布于 2024-09-18 07:37:02 字数 3233 浏览 6 评论 0原文

package com.example.helloandroid;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;

public class HelloAndroid extends TabActivity {
/** Called when the activity is first created. */
private ListView mainListView ;  
private ArrayAdapter<String> listAdapter ;
@Override


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

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1"));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2"));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3"));

    mTabHost.setCurrentTab(0);

 // Find the ListView resource.   
    mainListView = (ListView) findViewById( R.id.lstMain);  

    // Create and populate a List of planet names.  
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                                      "Jupiter", "Saturn", "Uranus", "Neptune"};    
    ArrayList<String> planetList = new ArrayList<String>();  
    planetList.addAll( Arrays.asList(planets) );  

    // Create ArrayAdapter using the planet list.  
   listAdapter = new ArrayAdapter<String>(this, R.layout.main, planetList);  

    // Set the ArrayAdapter as the ListView's adapter.  
    mainListView.setAdapter( listAdapter ); 


 // End


}

是我的 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">
        <TextView 
            android:id="@+id/textview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a tab" />
        <TextView 
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is another tab" />
        <TextView 
            android:id="@+id/textview3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a third tab" />
        <ListView
            android:id="@+id/lstMain" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">

package com.example.helloandroid;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;

public class HelloAndroid extends TabActivity {
/** Called when the activity is first created. */
private ListView mainListView ;  
private ArrayAdapter<String> listAdapter ;
@Override


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

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1"));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2"));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3"));

    mTabHost.setCurrentTab(0);

 // Find the ListView resource.   
    mainListView = (ListView) findViewById( R.id.lstMain);  

    // Create and populate a List of planet names.  
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                                      "Jupiter", "Saturn", "Uranus", "Neptune"};    
    ArrayList<String> planetList = new ArrayList<String>();  
    planetList.addAll( Arrays.asList(planets) );  

    // Create ArrayAdapter using the planet list.  
   listAdapter = new ArrayAdapter<String>(this, R.layout.main, planetList);  

    // Set the ArrayAdapter as the ListView's adapter.  
    mainListView.setAdapter( listAdapter ); 


 // End


}

}

and here is the code for my xml file.

<?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">
        <TextView 
            android:id="@+id/textview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a tab" />
        <TextView 
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is another tab" />
        <TextView 
            android:id="@+id/textview3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a third tab" />
        <ListView
            android:id="@+id/lstMain" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2024-09-25 07:37:02

也许如果您看一下我的两篇博客文章,它可能会给您一些有用的想法。
查看多个 android-activities
另请参阅配置 Android Listview 文章。
希望有帮助。

maybe if you look at two of my blog articles it might give you some helpful ideas.
check out multiple-android-activities
also take a look at the Configuring Android Listview article as well.
Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文