如何设置ActionBar的导航选项卡自定义视图,使标签适应其高度?

问题描述 投票:5回答:2

我使用的是ActionBar,我想设置自定义View在导航选项卡。

标签的高度似乎是固定的,而我的自定义View较大,所以它不适合。

我尝试自定义样式,如下图所示,但它不会使标签高...

怎样才可以有标签的高度适应我的自定义View大小?

(我知道我可以使用TabsetIconsetTitle在我的情况,但我仍然想使用自定义View

styles.xml

<resources>   
    <style name="AppBaseTheme" parent="android:Theme.Holo">
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarTabStyle">@style/MyTabStyle</item>
    </style>

     <style name="MyTabStyle" parent="@android:Widget.ActionBar.TabView">
        <item name="android:height">85dp</item>
    </style>
</resources>

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:scaleType="centerInside" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:maxLines="1" />

</LinearLayout>

main activity.Java

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.

            LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.custom_tab, null);

            ImageView icon = (ImageView) view.findViewById(R.id.icon);
            icon.setImageResource(R.drawable.about);

            TextView title = (TextView) view.findViewById(R.id.title);
            title.setText("About");


            actionBar.addTab(actionBar.newTab()
                    //.setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setCustomView(view)
                    .setTabListener(this));
        }
    }

    /*****/
}
android android-actionbar
2个回答
0
投票

此保持顶部的动作条,但我不知道为什么!

ActionBar.DISPLAY_SHOW_HOME

在动作条留在上面:

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  | ActionBar.DISPLAY_SHOW_HOME);

标签下去的动作条的顶部:

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM 

0
投票

据我所知,你不能改变操作栏选项卡的高度。但是你可以通过使用TabHost与ViewPager一起达到预期的行为。这样,您就可以添加自定义视图您TabIndicators,也有刷卡功能。

© www.soinside.com 2019 - 2024. All rights reserved.