【问题标题】:Android ActionBar.Tab setCustomView() doesn't fill_parentAndroid ActionBar.Tab setCustomView() 不填充父
【发布时间】:2013-11-03 15:52:11
【问题描述】:

我在此布局中使用 ActionBar.Tab setCustomView() 方法:

<?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>

这是我设置 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>

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

如何让我的自定义视图填满整个标签并正常工作?

我正在使用适用于 Android 2.3 的支持包 v7

【问题讨论】:

  • 能否请您发布您的活动 xml 布局?
  • 完成。我已经更新了我的问题

标签: android tabs android-actionbar


【解决方案1】:

我自己遇到了这个问题并想出了解决方案。您应该为 tabview 创建一个清除背景和填充的样式,并在您的主题中使用它。

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>

主题.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>

【讨论】:

【解决方案2】:

为每个创建的选项卡对象添加 layoutParams:

..    
tab.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
actionBar.addTab(tab);

【讨论】:

  • 谢谢,你救了我!虽然我的情况有点不同——我尝试设置 ActionBar 的自定义视图(它的根)layout_width="fill_parent",但它不起作用(就像它设置为“wrap_content”一样工作)。 cView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 解决了问题
  • 我忘了说我在actionBar.setCustomView(cView);之后打电话给cView.setLayoutParams(...)
【解决方案3】:

在处理actionbarcompat的setCustomView时,你必须依赖android:marginandroid:padding

请记住,根元素的内边距、宽度、高度和边距似乎被忽略了。

【讨论】:

    【解决方案4】:

    如果您仍有问题,我找到了获取整个空间的方法:ActionBar Tab with custom View not centered

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多