简体   繁体   English

使用顶部的标签显示不同的活动

[英]Show different Activities using Tabs at top

I am working on a project, where i need to show different views in a single window by selecting different tabs from the top. 我正在做一个项目,在这里我需要通过从顶部选择不同的选项卡在一个窗口中显示不同的视图。

I have googled this topic a lot, but i dont know what exact terminology is used for this view, so finding it difficult to search it. 我已经在这个主题上搜索了很多,但是我不知道该视图使用了什么确切的术语,因此很难搜索它。 I am working on a view just like this, 我正在像这样工作

标签活动

I have gone through several examples like this 我已经通过像几个例子了

and this 这个

but i could not get desired answer. 但我无法得到想要的答案。

Any usefull link that could direct me to implement this view would be helpfull. 任何可以指导我实施此视图的有用链接都将有所帮助。

tab_host.xml 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>

tabactivity 制表活动

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();
}
 }

You can define your own layout for PostImage,PostVideo,PostText. 您可以为PostImage,PostVideo,PostText定义自己的布局。 Customize this according to your needs. 根据您的需要自定义。

There are lot of types of TabHost in android. android中有很多TabHost类型。 You can use that by default one or you can customize it with whatever you required with it. 您可以默认使用它,也可以根据需要进行自定义。

Simply have a look at this example. 只需看一下这个例子。 It will provide you the default one of android tabHost Take a look at below code which will provide a TabHost as per your needs - 它会为您提供的Android的默认的一个tabHost在看看下面的代码,这将提供一个TabHost按您的需求-

Main.xml 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: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>

And, take a look at here also. 而且,也请在这里看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM