我正在做一个项目,我需要通过从顶部选择不同的选项卡在单个窗口中显示不同的视图。
我在谷歌上搜索了很多这个主题,但我不知道这个视图使用了什么确切的术语,所以很难搜索它。我正在研究这样的观点,
我已经经历了几个这样的例子
而这个
但我无法得到想要的答案。
任何可以指导我实现此视图的有用链接都会有所帮助。
tab_host.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0F5EC6"
android:orientation="horizontal" >
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:gravity="center"
android:text="ThirdEye"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:gravity="center"
android:text="Logout" />
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
选项卡活动
public class PostTabActivity extends TabActivity implements TabHost.OnTabChangeListener {
private TabHost mTabHost;
Button back,logout;
private String sessid,Name;
double lati,longi;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
Bundle extras = getIntent().getExtras();
if (extras != null) {
sessid = extras.getString("sid");
lati = extras.getDouble("EXTRA_latitude");
longi = extras.getDouble("EXTRA_longitude");
Name= extras.getString("NICK_NAME");
System.out.println("........................"+lati+".................."+longi);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab_host);
logout = (Button) findViewById(R.id.logout);
logout.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
new Logout(PostTabActivity.this,sessid);
//finish();
break;
}
return true;
}
});
back= (Button) findViewById(R.id.back);
back.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
Intent myIntent = new Intent(PostTabActivity.this, GetPost.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtra("EXTRA_SESSION_ID", sessid);
myIntent.putExtra("EXTRA_latitude",lati);
myIntent.putExtra("EXTRA_longitude", longi);
myIntent.putExtra("NICK_NAME", Name);
startActivity(myIntent);
finish();
break;
}
return true;
}
}
);
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
/*Tab One */
Intent mIntent= new Intent("proto.thirdeye.PostText");
mIntent.setClass(this, PostText.class);
mIntent.putExtra("sid",sessid);
mIntent.putExtra("EXTRA_latitude",lati);
mIntent.putExtra("EXTRA_longitude", longi);
mIntent.putExtra("NICK_NAME", Name);
mTabHost.addTab(mTabHost.newTabSpec("text1")
.setIndicator("Text",
getResources().getDrawable(R.drawable.image))
.setContent(mIntent));
/*Tab Two */
Intent mIntent1 = new Intent("proto.thirdeye.PostImage");
mIntent1.setClass(this, PostImage.class);
mIntent1.putExtra("sid",sessid);
mIntent1.putExtra("EXTRA_latitude",lati);
mIntent1.putExtra("EXTRA_longitude", longi);
mIntent1.putExtra("NICK_NAME", Name);
mTabHost.addTab(mTabHost.newTabSpec("image1")
.setIndicator("Image",
getResources().getDrawable(R.drawable.image))
.setContent(mIntent1));
/*Tab Three */
Intent mIntent2 = new Intent("proto.thirdeye.PostVideo");
mIntent2.setClass(this, PostVideo.class);
mIntent2.putExtra("sid",sessid);
mIntent2.putExtra("EXTRA_latitude",lati);
mIntent2.putExtra("EXTRA_longitude", longi);
mIntent2.putExtra("NICK_NAME", Name);
mTabHost.addTab(mTabHost.newTabSpec("video")
.setIndicator("Video",
getResources().getDrawable(R.drawable.image))
.setContent(mIntent2));
mTabHost.setCurrentTab(0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(PostTabActivity.this, GetPost.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtra("EXTRA_SESSION_ID", sessid);
myIntent.putExtra("EXTRA_latitude",lati);
myIntent.putExtra("EXTRA_longitude", longi);
myIntent.putExtra("NICK_NAME", Name);
startActivity(myIntent);
finish();
return;
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
Log.i("Aru"," "+tabId);
Activity activity = getLocalActivityManager().getActivity(tabId);
if (activity != null) {
activity.onWindowFocusChanged(true);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("sid",sessid);
savedInstanceState.putDouble("EXTRA_latitude",lati);
savedInstanceState.putDouble("EXTRA_longitude",longi);
savedInstanceState.putString("NICK_NAME", Name);
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
sessid = savedInstanceState.getString("sid");
lati = savedInstanceState.getDouble("EXTRA_latitude");
longi = savedInstanceState.getDouble("EXTRA_longitude");
Name = savedInstanceState.getString("NICK_NAME");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//MyApplication.activityPaused();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//MyApplication.activityResumed();
}
}
您可以为 PostImage、PostVideo、PostText 定义自己的布局。根据您的需要自定义它。
android中有很多类型TabHost
。您可以默认使用它,也可以根据需要对其进行自定义。
简单地看一下这个例子。它将为您提供默认的 androidtabHost
看看下面的代码,它将TabHost
根据您的需要提供 -
主.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">
</FrameLayout>
</LinearLayout>
</TabHost>
而且,也请看这里。