在选项卡视图下显示列表

发布于 2024-12-14 00:00:41 字数 11543 浏览 2 评论 0原文

我正在尝试在选项卡式视图的选项卡下显示自定义列表。 我的代码在这里:

**manifest:**

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.clinical.testapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk android:minSdkVersion="13" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >

            <activity android:name="com.clinical.testapp.ArtistsActivity" />
            <activity android:name="com.clinical.testapp.AlbumsActivity" />
            <activity android:name="com.clinical.testapp.SongsActivity" />
            <activity android:name="com.clinical.testapp.DatabaseActivity" />
            <activity android:name="com.clinical.testapp.AppsManager" />
            <activity android:name="com.clinical.testapp.AppListActivity" />

            <activity android:name=".HelloTabWidget" android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar">
              <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                 </intent-filter>
            </activity>

        </application>

    </manifest>

**main.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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="30dp" >

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

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_horizontal"
                 >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="20dp" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

</TabHost>

**app_row.xml:**

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:padding="6dip">
         <ImageView
             android:id="@+id/icon"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_marginRight="6dip"
             android:src="@drawable/ic_launcher" />
         <LinearLayout
             android:orientation="vertical"
             android:layout_width="0dip"
             android:layout_weight="1"
             android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/toptext"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:gravity="center_vertical"
             />
             <TextView
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1" 
                 android:id="@+id/bottomtext"
                 android:singleLine="true"
                 android:ellipsize="marquee"
             />
         </LinearLayout>
    </LinearLayout>

**HelloTabWidget.java:**

    package com.clinical.testapp;

    import java.util.ArrayList;

    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.ScrollView;
    import android.widget.TabHost;

    public class HelloTabWidget extends TabActivity {
        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);

            intent = new Intent().setClass(this, DatabaseActivity.class);
            spec = tabHost.newTabSpec("DB").setIndicator("DB",
                    res.getDrawable(R.drawable.ic_tab_db))
                    .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, AppsManager.class);
            spec = tabHost.newTabSpec("Apps").setIndicator("Apps",
                    res.getDrawable(R.drawable.ic_tab_apps))
                    .setContent(intent);
            tabHost.addTab(spec);

            spec = tabHost.newTabSpec("Appslist").setIndicator("Appslist",
                    res.getDrawable(R.drawable.ic_tab_applist));
            spec.setContent(new TabHost.TabContentFactory(){
                public View createTabContent(String tag)
                {
                 // -- this tab contains a single control - the listview -- //


                    /*  ScrollView sv = new ScrollView(HelloTabWidget.this);
                    ViewGroup.LayoutParams svLp = new ScrollView.LayoutParams(
                            ViewGroup.LayoutParams.FILL_PARENT,
                            ViewGroup.LayoutParams.FILL_PARENT);

                    LinearLayout ll = new LinearLayout(HelloTabWidget.this);
                    ll.setLayoutParams(svLp);
                    */

                    ListView ls1 = new ListView(HelloTabWidget.this);
                    //ls1.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT,ListView.LayoutParams.FILL_PARENT));
                    ArrayList<App> m_apps = new ArrayList<App>();
                    App a1 = new App();
                    a1.setAppName("SF services");
                    a1.setAppDesc("Pending");
                    App a2 = new App();
                    a2.setAppName("SF Advertisement");
                    a2.setAppDesc("Completed");
                    m_apps.add(a1);
                    m_apps.add(a2);
                    m_apps.add(new App("app3 name","app3 desc"));
                    AppAdapter m_adapter = new AppAdapter(HelloTabWidget.this, R.layout.app_row, m_apps);
                   ls1.setAdapter(m_adapter);
                   ls1.setOnCreateContextMenuListener(HelloTabWidget.this);
                   return ls1;
                   /*
                   ll.addView(ls1);
                   sv.addView(ll);
                   return sv;
                   */
    //                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    //                      HelloTabWidget.this,
    //                       android.R.layout.simple_list_item_1,
    //                       new String[]{"item1","item2","item3","item4","item5","item6","item7"});
    //                
    //                
    //                 ls1.setAdapter(adapter); 
    //                 ls1.setOnCreateContextMenuListener(HelloTabWidget.this);
    //                 return ls1;
                }         
           });


            /*
            intent = new Intent().setClass(this, AppsListActivity.class);
            spec = tabHost.newTabSpec("Appslist").setIndicator("Appslist",
                    res.getDrawable(R.drawable.ic_tab_applist))
                    .setContent(intent);
            tabHost.addTab(spec);
            */

            tabHost.addTab(spec);


            for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
                tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 133;
            } 

            tabHost.setCurrentTab(2);
        }
    }

**and AppAdapter.java:**

    package com.clinical.testapp;

    import java.util.ArrayList;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;


    public class AppAdapter extends ArrayAdapter<App> {

             private ArrayList<App> items;

             public AppAdapter(Context context, int textViewResourceId, ArrayList<App> items) {
                     super(context, textViewResourceId, items);
                     this.items = items;
             }

             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
                     View v = convertView;
                     if (v == null) {
                         LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                         v = vi.inflate(R.layout.app_row, null);
                     }
                     App app = items.get(position);
                     if (app != null) {
                             TextView tt = (TextView) v.findViewById(R.id.toptext);
                             TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                             if (tt != null) {
                                   tt.setText("Name: "+app.getAppName());                            }
                             if(bt != null){
                                   bt.setText("Status: "+ app.getAppDesc());
                             }
                     }
                     return v;
             }
    }

但列表的结果是: 我看不到整个列表,它以某种方式停止显示列表的其余部分。 请帮忙!! 谢谢,布什

I'm trying to display a custom list under a tab in a tabbed view.
my code is here:

**manifest:**

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.clinical.testapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk android:minSdkVersion="13" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >

            <activity android:name="com.clinical.testapp.ArtistsActivity" />
            <activity android:name="com.clinical.testapp.AlbumsActivity" />
            <activity android:name="com.clinical.testapp.SongsActivity" />
            <activity android:name="com.clinical.testapp.DatabaseActivity" />
            <activity android:name="com.clinical.testapp.AppsManager" />
            <activity android:name="com.clinical.testapp.AppListActivity" />

            <activity android:name=".HelloTabWidget" android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar">
              <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                 </intent-filter>
            </activity>

        </application>

    </manifest>

**main.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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="30dp" >

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

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_horizontal"
                 >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="20dp" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

</TabHost>

**app_row.xml:**

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:padding="6dip">
         <ImageView
             android:id="@+id/icon"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_marginRight="6dip"
             android:src="@drawable/ic_launcher" />
         <LinearLayout
             android:orientation="vertical"
             android:layout_width="0dip"
             android:layout_weight="1"
             android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/toptext"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:gravity="center_vertical"
             />
             <TextView
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1" 
                 android:id="@+id/bottomtext"
                 android:singleLine="true"
                 android:ellipsize="marquee"
             />
         </LinearLayout>
    </LinearLayout>

**HelloTabWidget.java:**

    package com.clinical.testapp;

    import java.util.ArrayList;

    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.ScrollView;
    import android.widget.TabHost;

    public class HelloTabWidget extends TabActivity {
        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);

            intent = new Intent().setClass(this, DatabaseActivity.class);
            spec = tabHost.newTabSpec("DB").setIndicator("DB",
                    res.getDrawable(R.drawable.ic_tab_db))
                    .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, AppsManager.class);
            spec = tabHost.newTabSpec("Apps").setIndicator("Apps",
                    res.getDrawable(R.drawable.ic_tab_apps))
                    .setContent(intent);
            tabHost.addTab(spec);

            spec = tabHost.newTabSpec("Appslist").setIndicator("Appslist",
                    res.getDrawable(R.drawable.ic_tab_applist));
            spec.setContent(new TabHost.TabContentFactory(){
                public View createTabContent(String tag)
                {
                 // -- this tab contains a single control - the listview -- //


                    /*  ScrollView sv = new ScrollView(HelloTabWidget.this);
                    ViewGroup.LayoutParams svLp = new ScrollView.LayoutParams(
                            ViewGroup.LayoutParams.FILL_PARENT,
                            ViewGroup.LayoutParams.FILL_PARENT);

                    LinearLayout ll = new LinearLayout(HelloTabWidget.this);
                    ll.setLayoutParams(svLp);
                    */

                    ListView ls1 = new ListView(HelloTabWidget.this);
                    //ls1.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT,ListView.LayoutParams.FILL_PARENT));
                    ArrayList<App> m_apps = new ArrayList<App>();
                    App a1 = new App();
                    a1.setAppName("SF services");
                    a1.setAppDesc("Pending");
                    App a2 = new App();
                    a2.setAppName("SF Advertisement");
                    a2.setAppDesc("Completed");
                    m_apps.add(a1);
                    m_apps.add(a2);
                    m_apps.add(new App("app3 name","app3 desc"));
                    AppAdapter m_adapter = new AppAdapter(HelloTabWidget.this, R.layout.app_row, m_apps);
                   ls1.setAdapter(m_adapter);
                   ls1.setOnCreateContextMenuListener(HelloTabWidget.this);
                   return ls1;
                   /*
                   ll.addView(ls1);
                   sv.addView(ll);
                   return sv;
                   */
    //                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    //                      HelloTabWidget.this,
    //                       android.R.layout.simple_list_item_1,
    //                       new String[]{"item1","item2","item3","item4","item5","item6","item7"});
    //                
    //                
    //                 ls1.setAdapter(adapter); 
    //                 ls1.setOnCreateContextMenuListener(HelloTabWidget.this);
    //                 return ls1;
                }         
           });


            /*
            intent = new Intent().setClass(this, AppsListActivity.class);
            spec = tabHost.newTabSpec("Appslist").setIndicator("Appslist",
                    res.getDrawable(R.drawable.ic_tab_applist))
                    .setContent(intent);
            tabHost.addTab(spec);
            */

            tabHost.addTab(spec);


            for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
                tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 133;
            } 

            tabHost.setCurrentTab(2);
        }
    }

**and AppAdapter.java:**

    package com.clinical.testapp;

    import java.util.ArrayList;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;


    public class AppAdapter extends ArrayAdapter<App> {

             private ArrayList<App> items;

             public AppAdapter(Context context, int textViewResourceId, ArrayList<App> items) {
                     super(context, textViewResourceId, items);
                     this.items = items;
             }

             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
                     View v = convertView;
                     if (v == null) {
                         LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                         v = vi.inflate(R.layout.app_row, null);
                     }
                     App app = items.get(position);
                     if (app != null) {
                             TextView tt = (TextView) v.findViewById(R.id.toptext);
                             TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                             if (tt != null) {
                                   tt.setText("Name: "+app.getAppName());                            }
                             if(bt != null){
                                   bt.setText("Status: "+ app.getAppDesc());
                             }
                     }
                     return v;
             }
    }

but the result of the list is:
I cannot see the whole list, it's somehow stop displaying the rest of the list.
Please helpp!!
Thanks, Bush

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

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

发布评论

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

评论(1

孤千羽 2024-12-21 00:00:41

我遇到了问题,它在 main.xml 中。
我应该完成并在里面!不在外面!
我在这上面浪费了很多时间,我很高兴分享答案!
gr8日!
衬套。

I got my problem, it was in the main.xml.
i've should done the and insisde the ! not outside!
I've wasted much hours on it and i'm glad to share the answer!
gr8 day!
Bush.

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