【问题标题】:Android — Add new tab when button click like Google Chrome new button?Android — 像 Google Chrome 新按钮一样单击按钮时添加新标签?
【发布时间】:2012-10-17 08:50:56
【问题描述】:

一般来说,一切都在谷歌浏览器上运行。单击新选项卡按钮时,将生成一个新选项卡。同样,我想在 Android 浏览器中添加一个新选项卡。如何做到这一点——有人知道吗?

  1. 首先,在Android中可以吗?

  2. 如果可能,如何做到这一点?

当我单击+ 按钮时,应该会生成一个新选项卡。如何做到这一点?

【问题讨论】:

  • 您使用什么类型的标签? PagerTabStrip?操作栏中的选项卡? TabHostTabWidget? ViewPagerIndicator 的标签?还有什么?
  • @CommonsWare 我想要谷歌浏览器标签,就像我们点击按钮一样,新标签已打开....
  • 很好。请单击您的问题下方的“编辑”链接,然后修改问题以向我们显示您定义选项卡的代码。如果您没有这样的代码,请回答我在第一条评论中提出的问题。如果您不理解这个问题,您需要先决定如何显示您的标签,然后我们才能告诉您如何动态添加标签。
  • 你可以告诉我怎么做,我不知道
  • 我认为 CommmonsWare 的意思是添加 Tab 的实际方法取决于您使用的选项卡类型,操作栏中的选项卡或 TabHost。

标签: android button browser


【解决方案1】:

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

您的 Manifest.xml 文件

<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>

这是 Tab 活动的 activity_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: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 放入 Horizo​​ntalScrollView 中,这样当添加更多选项卡时,它可以滚动并且 Add 但在 Horizo​​ntalScrollView 之外。每次单击选项卡时,它都会在 TabWidget 中添加新选项卡。

这里是 tab_event.xml 的代码 这个布局将膨胀并添加到选项卡中。您可以更改样式并使其包含单个 Button,以便您也可以添加带有文本的可绘制图像。

<?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 文件

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 文件

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

【讨论】:

  • 每次在 BlankActivity 中为特定选项卡创建,以便在 TextView 中创建新的选项卡,这样您就可以在其中添加 WebView 而不是 TextView。或者您可以创建包含地址栏、webview 和搜索栏的布局并在空白活动中设置
  • 是的,对于单个选项卡容器,它会创建 Activity 的新实例,您可以使用相同的单个 xml。他们在记忆中的待遇也不同,所以不用担心。
  • 您可以在按钮布局中设置 minWidth 和 maxWidth,因为我已将其设置为 wrap_content,因此当创建更多选项卡时,按钮大小是根据文本设置的,您可以在 layout_width 或最小或最大宽度中设置跨度>
  • TabActivity 已弃用。最好不要使用它。
【解决方案2】:

对于按钮单击创建一个新的标签规范。

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

【讨论】:

    【解决方案3】:

    如下创建一个Activity:

    public class WebViewActivity extends Activity {
            @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                WebView wv=new WebView(this);
            setContentView(wv);
        }
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2013-06-30
      • 1970-01-01
      相关资源
      最近更新 更多