首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android -当按钮点击时添加新标签,就像Google Chrome的新按钮?

Android -当按钮点击时添加新标签,就像Google Chrome的新按钮?
EN

Stack Overflow用户
提问于 2012-10-27 14:44:06
回答 3查看 12.6K关注 0票数 17

总的来说,一切都可以在Google Chrome上运行。单击新选项卡按钮时,将生成一个新选项卡。同样,我想在Android浏览器中添加一个新的选项卡。如何做到这一点--有谁知道吗?

  1. 首先,安卓可以吗?
  2. 如果可以,怎么做?

当我单击+按钮时,应该会生成一个新的选项卡。该怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-31 13:37:36

好的,这是代码,但这只是一个例子,你可能需要根据你的需求修改代码。我在这里给你所有的文件代码,希望你得到答案。

您的Manifest.xml文件

代码语言:javascript
复制
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.dynamictab"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BlankActivity"/>
    </application>

</manifest>

这是用于选项卡活动的activity_main.xml文件

代码语言:javascript
复制
<?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: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:fillViewport="true"
                android:scrollbars="none" 
                android:layout_weight="1">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    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:layout_weight="0.1"
                android:text="Add"/>
        </LinearLayout>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="2dp" />
    </LinearLayout>

</TabHost>

将你的TabWidget放到HorizontalScrollView中,这样当更多的标签被添加时,它就可以滚动了,add but就在HorizontalScrollView的外面。每次单击选项卡时,都会在TabWidget中添加新的选项卡。

在这里,这个布局的tab_event.xml代码将膨胀并添加到标签中。您可以更改样式,其包含单个按钮,以便您可以添加可绘制的文本图像也。

代码语言:javascript
复制
<?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文件

代码语言:javascript
复制
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.java文件

代码语言:javascript
复制
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");
        setContentView(tv);
    }
}
票数 6
EN

Stack Overflow用户

发布于 2012-10-29 15:43:36

对于按钮,单击create a new tabspec。

代码语言:javascript
复制
TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
    //What ever thing you want to display inside the tab
    TextView text = new TextView(CONTEXT);
    text.setText("New tab");
    return(text);
    }
});
spec.setIndicator("New");
th.addTab(spec);

请参考以下youtube教程。这很简单。

http://www.youtube.com/watch?v=NcKSFlYEqYY

票数 2
EN

Stack Overflow用户

发布于 2012-11-03 20:46:33

创建一个活动,如下所示:

代码语言:javascript
复制
public class WebViewActivity extends Activity {
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            WebView wv=new WebView(this);
        setContentView(wv);
    }
}

现在使用@Partik的方法添加一个新选项卡,在每个+按钮上单击:

代码语言:javascript
复制
private void addTab(){
    Button tabBtn = new Button(MainActivity.this);
    tabBtn.setText("Tab "+tabIndex);
    Intent tabIntent = new Intent(MainActivity.this, WebViewActivity.class);

    setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13097931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档