Android的 - 添加新的选项卡时,按一下​​按钮,如谷歌Chrome浏览器的新按钮​​?按钮、选项卡、浏览器、Android

2023-09-12 09:34:50 作者:长发及腰那是贞子

在一般情况下,一切工作在谷歌浏览器。当点击新标签按钮,一个新的选项卡生成。以同样的方式,我想加入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?

首先,是有可能在Android的?

First, is it possible in Android?

如果可能的话,如何做到这一点?

If possible, how to do this?

当我点击 + 按钮,应该会产生一个新的标签。如何做到这一点?

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

推荐答案

确定这里是code,但它只是一个示例,你可能需要修改code按您的要求。我给你的所有文件code在这里希望你得到的答案。

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>

下面是标签活动的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所以当多个选项卡添加可滚动和添加,但不在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.

下面的$ C $下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);
    }
}