在选项卡式视图下显示列表
Posted
技术标签:
【中文标题】在选项卡式视图下显示列表【英文标题】:Displaying list under tabbed view 【发布时间】:2011-11-03 10:29:05 【问题描述】:我正在尝试在选项卡式视图的选项卡下显示自定义列表。 我的代码在这里:
**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_
android:layout_ >
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:padding="30dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_
android:layout_ />
<ScrollView
android:layout_
android:layout_ >
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_
android:layout_
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_
android:layout_
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_
android:layout_
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_weight="1"
android:layout_>
<TextView
android:id="@+id/toptext"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_
android:layout_
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;
但列表的结果是: 我看不到整个列表,它以某种方式停止显示列表的其余部分。 请帮忙!! 谢谢,布什
【问题讨论】:
我们对这段代码有什么用?提供xml布局文件 我提供了我拥有的所有代码,您只需向下滚动即可。顺便说一句,我解决了这个问题,请参阅下面的答案。 【参考方案1】:我遇到了问题,它在 main.xml 中。 我应该做的并且坚持!不在外面! 我已经浪费了很多时间,我很高兴分享答案! gr8 天! 布什。
【讨论】:
以上是关于在选项卡式视图下显示列表的主要内容,如果未能解决你的问题,请参考以下文章