在每个选项卡上添加按钮(X)以关闭所选选项卡

时间:2013-11-07 18:28:10

标签: java android android-tabhost android-button

根据我给出的代码,我可以添加标签

这是我的主要活动xml activity_main.xml

<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="5dp" >

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fillViewport="true"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="49dp"
                android:layout_marginLeft="0dip"
                android:layout_marginRight="0dip" />
        </HorizontalScrollView>

        <Button
            android:id="@+id/add_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add" />
    </LinearLayout>


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

`

tab_event.xml

<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_event" android:clickable="true" android:focusable="true" android:text="Default Tab" android:layout_width="wrap_content" android:layout_height="wrap_content" />

MainActivity.java

package com.example.tabweb2;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tabHost = getTabHost();

    addTab();

    ((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            tabIndex++;
            addTab();
        }
    });
}
private void addTab(){
    LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);

    Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
    tabBtn.setText("Tab "+tabIndex);
    Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);

    setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
protected void setupTab(View tabBtn, Intent setClass,String tag) {
    TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
    tabHost.addTab(setContent);
}}

使用所有标签打开blankactivity

BlankActivity.java

package com.example.tabweb2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class BlankActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(BlankActivity.this);
    tv.setText("Blank activity"+1);
    setContentView(tv);
}}

根据我给出的我可以添加新标签(添加按钮)

但我需要这样,这是否可以在每个标签上添加(X)按钮并关闭所选标签?

0 个答案:

没有答案