【问题标题】:How to add common button to all tabs of Tab Host in Android?如何将通用按钮添加到Android中Tab Host的所有选项卡?
【发布时间】:2012-03-14 18:51:00
【问题描述】:

我正在使用此代码。

private ViewGroup createTabbarView() {

        bookedZone = "None";
        bookedStand = "None";

        tabHost = new TabHost(this, null);
        tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        Button btn = new Button(this);
        btn.setText("Emergency");
        tabHost.addView(btn, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        tabWidget = new TabWidget(this);
        tabWidget.setId(android.R.id.tabs);
        tabHost.addView(tabWidget, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setId(android.R.id.tabcontent);
        frameLayout.setPadding(0, 100, 0, 0);
        tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // WRAP_CONTENT

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            public void onTabChanged(final String tabId) {
                if (tabId.equalsIgnoreCase("TripList")
                        && (AVL_Service.pref.getBoolean("BluetoothMeter", false) || AVL_Service.pref.getBoolean("VivotechDevice", false) || AVL_Service.pref.getBoolean(
                                "BlueBambooDevice", false))) {
                    if (mBluetoothAdapter == null)
                        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    final Context tempContext = getBaseContext();
                    if (mBluetoothAdapter == null) {
                        // Device does not support Bluetooth
                        Log.d(getClass().getSimpleName(), "Bluetooth not supported.");
                        Toast.makeText(tempContext, "Bluetooth not supported!", Toast.LENGTH_LONG).show();
                    } else {
                        if (!mBluetoothAdapter.isEnabled()) {
                            Log.d(getClass().getSimpleName(), "Bluetooth not enabled.");
                            Toast.makeText(tempContext, "Bluetooth not enabled, trying to enable", Toast.LENGTH_LONG).show();
                            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                            startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
                            // tabHost.getTabWidget().getChildAt(ZoneIndex).requestFocusFromTouch();
                        } else {

                            Message lmsg = new Message();
                            lmsg.obj = null;
                            lmsg.what = MsgType.TabberClick;
                            msgHandler.sendMessageDelayed(lmsg, 1000);

                        }// if bluetooth enabled
                    }// if bluetooth not supported
                }// if Bluetooth connection required
                else if (tabId.equalsIgnoreCase("Status")) {
                    setMeterStatus();
                }
            }// onTabChanged
        });

        tabHost.setup();

        // Status Tab
        TabSpec tabSpec = tabHost.newTabSpec("Status");
        tabSpec.setIndicator("Status", getResources().getDrawable(R.drawable.status));
        tabSpec.setContent(new TabContentFactory() {
            @Override
            public View createTabContent(String arg0) {

                return createStatusView();
            }
        });
        tabHost.addTab(tabSpec);

        // Zones Tab
        tabSpec = tabHost.newTabSpec("Zones");
        tabSpec.setIndicator("Zones", getResources().getDrawable(R.drawable.zones));
        tabSpec.setContent(new TabContentFactory() {

            @Override
            public View createTabContent(String arg0) {

                return createZoneView();
            }
        });
        tabHost.addTab(tabSpec);

        // Bids Tab
        bidTab = tabHost.newTabSpec("Bids");
        bidTab.setIndicator("Bids", getResources().getDrawable(R.drawable.icon + bidCount));
        bidTab.setContent(new TabContentFactory() {

            @Override
            public View createTabContent(String arg0) {

                return createBidsView();
            }
        });
        tabHost.addTab(bidTab);

        // TripList Tab
        tabSpec = tabHost.newTabSpec("TripList");
        tabSpec.setIndicator("TripList", getResources().getDrawable(R.drawable.triplist));
        tabSpec.setContent(new TabContentFactory() {

            @Override
            public View createTabContent(String arg0) {

                return createManifestView();
            }
        });
        tabHost.addTab(tabSpec);

        return tabHost;
    }

//标签视图

但是这个按钮隐藏了 Tabber 视图的选项卡。我想添加以在 tabber 视图上方添加此按钮。请帮助我..我遇到了很大的麻烦。 问候。

【问题讨论】:

    标签: android android-layout tabs android-tabhost tabwidget


    【解决方案1】:

    所以基本上你想要一个位于TabWidget 标签上方的按钮(或多个按钮),而不管选择了哪个标签?

    只需将您的布局文件更改为类似于此的内容(伪代码)...

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        ... >
        <LinearLayout
            android:orientation="vertical"
            ... >
            <LinearLayout
                android:orientation="horizontal"
                ... >
                <Button
                    ... />
            </LinearLayout>
            <TabWidget
                android:id="@android:id/tabs"
                ... />
            <FrameLayout
                android:id="@android:id/tabcontent"
                ... >
                ...
            </FrameLayout>
        </LinearLayout>
    </TabHost
    

    【讨论】:

    • 我已经尝试过了,但没有动态工作。你能告诉我如何动态地做到这一点,因为我没有使用 xml.Regards
    • 为什么不使用 XML?即使您需要动态创建 tabspec 内容,也没有理由不能将 XML 模板用于主视图,然后简单地生成 tabspec 内容。在我看来,你为自己做了很多工作
    猜你喜欢
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多