Android的ActionBar.Tab setCustomView()不FILL_PARENT [英] Android ActionBar.Tab setCustomView() doesn't fill_parent

查看:271
本文介绍了Android的ActionBar.Tab setCustomView()不FILL_PARENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ActionBar.Tab setCustomView()方法,用这个布局:

I'm using ActionBar.Tab setCustomView() method with this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_grey" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Test Tab" 
        android:textColor="@color/background_dark_green"/>

</RelativeLayout>

这是我的功能设置动作条:

this is my function setting the ActionBar:

public void setActionBar()
{
    ActionBar actionBar = getSupportActionBar();
    //actionBar.hide();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);        
    //set action bar navigation mode
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
    //set tabs      
    //home tab 
    Tab tab = actionBar.newTab().setText(TAB_HOME).setTabListener(new PicoTabListener<StartFragment>(this, StartFragment.class));       
    tab.setCustomView(R.layout.tab_background);
    actionBar.addTab(tab);
    //events tab
    tab = actionBar.newTab().setText(TAB_EVENTS).setTabListener(new PicoTabListener<EventsFragment>(this, EventsFragment.class));
    actionBar.addTab(tab);      
    //enter event code
    tab = actionBar.newTab().setText(TAB_CODE).setTabListener(new PicoTabListener<EnterCodeFragment>(this, EnterCodeFragment.class));
    actionBar.addTab(tab);      
}

和我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_basic_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_dark_green" >

</RelativeLayout>

结果接缝看起来像这样(左上角的选项卡以灰色背景):

The results seams to look like this (The upper left tab with the gray background):

我怎样才能让我的自定义视图,以填补整个标签和正常工作?

How can i make my custom view to fill whole tab and work properly ?

我使用的是支持包V7为Android 2.3

I'm using support package v7 for Android 2.3

推荐答案

恰好碰到了这个自己,并想出解决方案。你应该为清除背景和填充的TabView的创建一个样式,并用它在你的主题。

Just ran into this myself and figured out the solution. You should create a style for the tabview that clears the background and the padding and use it in your theme.

styles.xml:

styles.xml:

<style name="Custom.ActionBar.TabView.Empty" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@null</item>
    <item name="android:padding">0dp</item>
</style>

的themes.xml:

themes.xml:

<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item>
    <item name="actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item>
</style>

这篇关于Android的ActionBar.Tab setCustomView()不FILL_PARENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆