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

由网友(青衫如故)分享简介:我使用了动作条,我想设置在导航选项卡自定义查看。标签的高度似乎是固定的,而我的自定义查看较大,所以它并不适合。我尝试如下图所示,自定义风格,但它不会使标签更高... 我怎么能有标签的高度适应我的自定义查看尺寸?(我知道我可以使用设置页的的setIcon 和的setTitle 在我的情况,但我还是想用一个自定义的查看)s...

我使用了动作条,我想设置在导航选项卡自定义查看

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

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

我怎么能有标签的高度适应我的自定义查看尺寸?

(我知道我可以使用设置页的setIcon 的setTitle 在我的情况,但我还是想用一个自定义的查看

styles.xml

 <资源>
    <样式名称=AppBaseTheme父=机器人:Theme.Holo>
    < /风格>
    <样式名称=AppTheme父=AppBaseTheme>
        <项目名称=机器人:actionBarTabStyle> @风格/ MyTabStyle< /项目>
    < /风格>

     <样式名称=MyTabStyle父=@安卓Widget.ActionBar.TabView>
        <项目名称=机器人:身高> 85dp< /项目>
    < /风格>
< /资源>
 

custom_tab.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:重力=center_horizo​​ntal>

    < ImageView的
        机器人:ID =@ + ID /图标
        机器人:layout_width =60dp
        机器人:layout_height =60dp
        机器人:scaleType =centerInside/>

    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_horizo​​ntal
        机器人:MAXLINES =1/>

< / LinearLayout中>
 

MainActivity.java

 公共类MainActivity扩展FragmentActivity工具
ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        //设置操作栏。
        最后的动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //创建将返回一个片段为三个的适配器
        //应用程序的主要部分。
        mSectionsPagerAdapter =新SectionsPagerAdapter(
                getSupportFragmentManager());

        //设置的ViewPager与部分适配器。
        mViewPager =(ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        //当不同部分之间的刷卡,选择相应的
        //标签。我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有
        //引用的标签。
        mViewPager
        .setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
            @覆盖
            公共无效onPageSelected(INT位置){
                actionBar.setSelectedNavigationItem(位置);
            }
        });

        //对于每个应用程序的章节中,添加一个标签,操作栏。
        的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
            //创建与对应于由所定义的页标题文本标签
            //适配器。同时,指定此活动的对象,它实现
            //该TabListener界面,回调(监听程序)进行时
            //这个选项卡中选择。

            的LinearLayout视图=(的LinearLayout)getLayoutInflater()膨胀(R.layout.custom_tab,空)。

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

            TextView的标题=(TextView中)view.findViewById(R.id.title);
            title.setText(关于);


            actionBar.addTab(actionBar.newTab()
                    //.setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setCustomView(视图)
                    .setTabListener(本));
        }
    }

    / ***** /
}
 
全屏显示课程章节

解决方案

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

I'm using the ActionBar and I want to set a custom View in the navigation tabs.

The height of the tabs seem to be fixed, and my custom View is larger, so it does not fit.

I try to customize the style as shown below, but it does not make the tabs higher...

How can I have the height of the tabs adapt to my custom View size?

(I know I could use Tab's setIcon and setTitle in my case, but I still want to use a custom 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>

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

    /*****/
}

解决方案

AFAIK, you can't change the height of the action bar tabs. But you can achieve the intended behaviour by using a TabHost along with a ViewPager. This way you can add custom views to your TabIndicators and also have the swiping functionality.

阅读全文

相关推荐

最新文章