如何在 Android 中的 TabView 上方添加自定义操作栏?

How to add custom actionbar above TabView in Android?

我的 classes 中有一个 TabView。现在我想在 tabview 上方放置一个自定义操作栏。为此,我正在使用 ActionBarSherlock。所以现在我的问题是我应该扩展 SherlockActivity 以便使用 Actionbar。但是我需要扩展 TabActivity 来实现 Tabs。我该如何解决这个问题?有人请帮忙。

这是我的class。

 public class ActivityRestaurantMenuListTab extends TabActivity {
        TabHost tabHost;
        HorizontalScrollView mHorizontalScrollView;
        int tabnum;
        String RestaurantName="Restaurant 1";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.restaurant_menu_list_tab);

            ActionBar actionBar = getSupportActionBar();

            View actionBarView = getLayoutInflater().inflate(
                    R.layout.custom_actionbar_restaurant, null);
            ImageView back = (ImageView) actionBarView.findViewById(R.id.btn_back);
            ImageView table = (ImageView) findViewById(R.id.btn_table);
            ImageView basket = (ImageView) findViewById(R.id.btn_basket);
            TextView tableno = (TextView) findViewById(R.id.txt_table_no);
            TextView basketitems = (TextView) findViewById(R.id.txt_basket_items);
            TextView actionBarTitle = (TextView) actionBarView
                    .findViewById(R.id.txt_title);

            actionBarTitle.setText(RestaurantName);

            basket.setVisibility(View.VISIBLE);

            back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            actionBar.setCustomView(actionBarView);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

            Intent intent = getIntent();
            tabnum = intent.getIntExtra("category", 0);
            Toast.makeText(getApplicationContext(), "Selected tab is " + tabnum,
                    Toast.LENGTH_SHORT).show();

            tabHost = (TabHost) findViewById(android.R.id.tabhost);
            mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.hs);

            TabWidget widget = tabHost.getTabWidget();

            TabSpec tab1 = tabHost.newTabSpec("Item 1");
            TabSpec tab2 = tabHost.newTabSpec("Item 2");
            TabSpec tab3 = tabHost.newTabSpec("Item 3");
            TabSpec tab4 = tabHost.newTabSpec("Item 4");

            tab1.setIndicator("Item 1");
            tab1.setContent(new Intent(this, Activity.class));

            tab2.setIndicator("Item 2");
            tab2.setContent(new Intent(this, Activity.class));

            tab3.setIndicator("Item 3");
            tab3.setContent(new Intent(this, Activity.class));

            tab4.setIndicator("Item 4");
            tab4.setContent(new Intent(this, Activity.class));

            tabHost.addTab(tab1);
            tabHost.addTab(tab2);
            tabHost.addTab(tab3);
            tabHost.addTab(tab4);

            float scale = getResources().getDisplayMetrics().density;
            final double tabWidth = (int) (150 * scale + 0.5f);

            for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
                tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams().width = (int) tabWidth;
            }

            final double screenWidth = getWindowManager().getDefaultDisplay()
                    .getWidth();

            tabHost.setOnTabChangedListener(new OnTabChangeListener() {

                @Override
                public void onTabChanged(String tabId) {
                    int nrOfShownCompleteTabs = ((int) (Math.floor(screenWidth
                            / tabWidth) - 1) / 2) * 2;
                    int remainingSpace = (int) ((screenWidth - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);

                    int a = (int) (tabHost.getCurrentTab() * tabWidth);
                    int b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
                    int offset = (a - b) - remainingSpace;

                    mHorizontalScrollView.scrollTo(offset, 0);
                }
            });

            for (int i = 0; i < widget.getChildCount(); i++) {
                View v = widget.getChildAt(i);


                TextView tv = (TextView) v.findViewById(android.R.id.title);
                tv.setAllCaps(false);
                tv.setTextSize(15);

                if (tv == null) {
                    continue;
                }
                tabHost.setCurrentTab(tabnum);
                v.setBackgroundResource(R.drawable.tabselector);
                tabHost.getTabWidget().setDividerDrawable(null);

            }

        }



    }

尝试在 xml 文件中的 tabview 上方添加宽度匹配父级和高度包裹内容的 Relativelayout 或线性布局我们可以提供背景颜色,我们可以将图标放置在此顶部虚拟操作栏布局中并且可以避免扩展夏洛克 activity

示例xml 文件可以是

 <RelativeLayout 
            android:layout_height="@dimen/titlebarsize"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_alignParentTop="true"
            android:id="@+id/topbar"
            >
            <TextView 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerInParent="true"
                android:id="@+id/title"
                android:text="@string/steptwotitle"
                   style="@style/actionbarphonetextviewstyle"
                />

             <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/back"
                android:id="@+id/back"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_centerVertical="true"
                android:background="@null"
                android:visibility="gone"
                />
            <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/sidemenutest"
                android:id="@+id/sidemenu"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:layout_centerVertical="true"
                android:background="@null"
                />
        </RelativeLayout>


        <ScrollView 
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_below="@+id/topbar"
            android:layout_centerHorizontal="true"

            >

class代码

protected void onCreate(Bundle savedInstanceState) {
      back=(ImageButton)findViewById(R.id.back);
            back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent in=new Intent(CommentsPage.this,Playmyfile.class);
                    startActivity(in);
                }
            });
    }

在您的 activity 标签内的清单中添加

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"