Android. Добавить новую вкладку при нажатии кнопки, например, кнопку Google Chrome. - программирование
Подтвердить что ты не робот

Android. Добавить новую вкладку при нажатии кнопки, например, кнопку Google Chrome.

В общем, все работает в Google Chrome. При нажатии кнопки новой вкладки создается новая вкладка. Точно так же я хочу добавить новую вкладку в браузер Android. Как это сделать - есть ли у кого-нибудь идеи?

  • Во-первых, возможно ли это в Android?

  • Если возможно, как это сделать?

    enter image description here

Когда я нажимаю кнопку +, необходимо создать новую вкладку. Как это сделать?

4b9b3361

Ответ 1

Ok Вот код, но это пример, вам может понадобиться изменить код в соответствии с вашим требованием. Я даю вам весь код файлов, надеюсь, что вы получите ответ.

Ваш файл 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>

вот ваш файл 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 в HorizontalScrollView, чтобы при добавлении дополнительных вкладок он мог прокручивать, а Add, но не в стороне от HorizontalScrollView. Каждый раз, когда вы нажимаете на вкладку, она добавляет новую вкладку в TabWidget.

Вот код для 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

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

Ответ 2

Для нажатия кнопки создайте новый tabspec.

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

Создайте действие, как показано ниже:

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