Android — 像 Google Chrome 新按钮一样单击按钮时添加新标签? [英] Android — Add new tab when button click like Google Chrome new button?

查看:29
本文介绍了Android — 像 Google Chrome 新按钮一样单击按钮时添加新标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?

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

  1. First, is it possible in Android?

如果可能,怎么做?

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

When I click on the + button, a new tab should be generated. How to do this?

推荐答案

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

Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.

您的 Manifest.xml 文件

Your Manifest.xml file

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

here is your activity_main.xml file for Tab activity

<?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 中添加新标签.

Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.

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

Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.

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

Here is your MainActivity.java file

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

And here is the BlankActivity.java file

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

这篇关于Android — 像 Google Chrome 新按钮一样单击按钮时添加新标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆