android - 如何在ActionBar的navigation tabs中设置自定义View,并让tabs自适应高度?

标签 android android-actionbar

我正在使用 ActionBar,我想在导航选项卡中设置自定义 View

标签的高度似乎是固定的,而我自定义的 View 更大,所以放不下。

我尝试自定义如下所示的样式,但它并没有使标签变高...

如何让选项卡的高度适应我自定义的 View 大小?

(我知道我可以在我的例子中使用 TabsetIconsetTitle,但我仍然想使用自定义的 查看)

enter image description here

样式.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>

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

    /*****/
}

最佳答案

这使操作栏保持在顶部,但我不知道为什么!

ActionBar.DISPLAY_SHOW_HOME

actionbar 保持在顶部:

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

选项卡位于操作栏的顶部:

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM 

关于android - 如何在ActionBar的navigation tabs中设置自定义View,并让tabs自适应高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20128836/

相关文章:

java - RecyclerView 在其他 View 之上没有触摸事件(覆盖)

java - Android 图像按钮 fragment

java - ARCore TransformableNode 拖动后将 localPosition 设置为 [x=0.0, y=0.0, z=0.0]

android - 在android中自定义操作栏

android - 是否可以在 ActionBar 上使用 TransitionDrawable?

java - Android 上连续按 2 个按键?

android - 具有可滚动父级的不可滚动 RecyclerView

android - 使用 Jeremy Feinstein 滑动条和操作栏 Sherlock 在 R.java 中缺少值

android - 将我的 ActionBar 放在底部

android - 操作栏 Sherlock 更改标题颜色