我正努力让listview和tabactivity一起工作。有人可以告诉我在1活动文件中让这两个一起工作的正确方法,但是有两个类(我认为它应该是如何工作的)
布局如下
________________________________________
| _____ _____ _____ _____ |
| | btn | | btn | | btn | | btn | |
| |_____| |_____| |_____| |_____| |
|______________________________________|
| | |
| | |
| | |
| list | tab contents |
| | |
| view | |
| | |
| contents | |
| | |
| here | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|______________|_______________________|
| ____ ____ | | | |
| |NXT ||PRV | | TAB1 | TAB2 | TAB3 |
| |____||____| | | | |
|______________|______|_______|________|
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="125dp"
android:layout_marginTop="100dp" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="310dp" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true" >
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_marginRight="203dp"
android:layout_marginTop="100dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp" >
<TextView
android:id="@android:id/textview_peoples"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:maxWidth="70dp"
android:minHeight="60dp"
android:minWidth="55dp"
android:text="Prev"
android:textSize="13sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button1"
android:layout_weight="1"
android:maxWidth="70dp"
android:minHeight="60dp"
android:minWidth="55dp"
android:text="Next"
android:textSize="13sp" />
</RelativeLayout>
<Button
android:id="@+id/btn_category_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Back" />
<Button
android:id="@+id/btn_category_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btn_category_back"
android:text="Search" />
<Button
android:id="@+id/btn_category_ts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btn_category_search"
android:text="TS" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btn_category_ts"
android:text="Button" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_marginBottom="360dp"
android:layout_marginTop="50dp"
android:orientation="vertical"
android:textAlignment="center"
android:layout_gravity="center" >
<TextView
android:id="@+id/textView_ppp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="9dp"
android:background="@color/greyish"
android:text="PPP"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</RelativeLayout>
我已经尝试过androidituts的各种示例,其中一些在stackoverflow上列出,但根本无法让它们一起工作。我需要两者来显示内容。如果我更改类文件,可以让它们独立工作,既可以显示列表视图的内容,也可以更改为显示选项卡视图。
我在下面用于tabview的类代码
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class subCategories_peoples extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_peoples);
TabHost tabHost = getTabHost();
// Tab for Config
TabSpec personspec = tabHost.newTabSpec("Person");
personspec.setIndicator("Person");
Intent personIntent = new Intent(this, Category_personActivity.class);
personspec.setContent(personIntent);
// Tab for Family
TabSpec familyspec = tabHost.newTabSpec("Family");
// setting Title and Icon for the Tab
familyspec.setIndicator("Family");
Intent familyIntent = new Intent(this, Category_familyActivity.class);
familyspec.setContent(familyIntent);
// Tab for History
TabSpec historyspec = tabHost.newTabSpec("History");
historyspec.setIndicator("History");
Intent historyIntent = new Intent(this, Category_historyActivity.class);
historyspec.setContent(historyIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(peoplespec); // Adding person tab
tabHost.addTab(familyspec); // Adding family tab
tabHost.addTab(historyspec); // Adding history tab
}
}
我用于列表视图的类代码:
import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SavedSearches extends ListActivity
{
// defining array to store string resources
String[] various_peoples;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// storing string resources into Array
various_peoples = getResources().getStringArray(R.array.various_people);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>
(this,R.layout.category_peoples, R.id.textview_peoples,various_peoples));
}
}
活动的类代码加载以上所有内容如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class subCategories extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.btn_subcategories);
Button btn_SubCategories_PPP = (Button) findViewById(R.id.btn_SubCategories_PPP);
btn_SubCategories_PPP.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), subCategories_peoples.class);
startActivityForResult(intent, 1);
}
})
;
}
}
最终,我需要列表视图和tabview一起工作,如果我选择与列表视图不同的东西,它应该加载一组新的选项卡内容,但我将自己处理/尝试。
提前致谢,