【问题标题】:Action Bar Tabs CustomView too long操作栏选项卡 CustomView 太长
【发布时间】:2013-07-06 14:20:48
【问题描述】:

在最终让每个 CustomView 完全包裹 Tab 父级之后,我遇到了 Tab 太长并且将 Action Bar 变成滑块的问题。我的可绘制对象的宽度为 150px ,这应该没问题,因为我测试了三星 Galaxy S2 屏幕的 px 宽度并得到了 480px(480/3 个标签 = 160px 每个)。

截图:


设置:每个选项卡都由相同的RelativeLayout 设置,该RelativeLayout 具有附加到布局的不同drawawble 资源。然后将该布局传递给 Tab 的 setCustomView()。我不认为每个选项卡都需要一个 ImageView,除非这样可以解决我的问题。为了将自定义视图放置在 Tab 上,已经进行了一些样式配置更改,例如删除 TabView 的左右填充。


代码(不使用 ImageView,在 RelativeLayout 上设置可绘制资源)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="56dp"
    android:layout_margin="0dp"
    android:paddingBottom="2dp" >

<!--     <ImageView 
        android:id="@+id/ivCustomTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:contentDescription="@string/tabsDescription"
        android:duplicateParentState="true" /> -->

</RelativeLayout>


//setting up tabs + custom views on tabs
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout discoverTabLayout = (RelativeLayout) inflater.inflate(R.layout.actionbar_tabs, null);
    RelativeLayout tagsTabLayout = (RelativeLayout) inflater.inflate(R.layout.actionbar_tabs, null);
    RelativeLayout badgeTabLayout = (RelativeLayout) inflater.inflate(R.layout.actionbar_tabs, null);
    //ImageView customTabImg = (ImageView) customTabLayout.findViewById(R.id.ivCustomTab);  //re-setting this ImageView for each tab

    ActionBar.Tab discoverTab = actionBar.newTab();//.setText("Discover").setIcon(R.drawable.selector_tabicon_discover);//.setCustomView(drawable.selector_tab_discover);
    //customTabImg.setImageResource(R.drawable.selector_tab_discover);
    discoverTabLayout.setBackgroundResource(R.drawable.selector_tab_discover);
    discoverTab.setCustomView(discoverTabLayout);


<!-- Application theme. -->
<style name="Theme.AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:textSize">18sp</item>
    <item name="actionBarTabStyle">@style/Theme.TabsNoPadding</item>
    <item name="android:actionBarTabStyle">@style/Theme.TabsNoPadding</item>
</style>

<style name="Theme.TabsNoPadding" parent="@style/Widget.Sherlock.ActionBar.TabView">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
    <item name="android:minWidth">0dp</item>
</style>


我的尝试:

  • 将 android:layout_width="120dp" 添加到 RelativeLayout 以缩短选项卡,但宽度保持不变。
  • 重新调整图像大小并减少总长度 45 像素,没有变化。如果我将它们设置得太小,它们将无法覆盖标签主机。
  • 我还注意到,customView(s) 选项卡大部分时间在 onCreate() 上消失。不确定这是否与我不想要的选项卡滚动布局的渲染有关。

如果需要,我可以发布我的主要活动代码。

【问题讨论】:

  • @Pratik 已发布。有什么建议吗?

标签: android android-layout actionbarsherlock android-actionbar android-custom-view


【解决方案1】:
  1. 我建议在 Android 中工作时考虑 dp(与密度无关的像素)而不是 px(像素)。

  2. 如果您想要自定义标签栏,我建议您简单地创建一个 LinearLayout,其宽度设置为 match_parent,高度由您选择(例如 48dp)。方向将设置为水平。

在 LinearLayout 中创建任意数量的 ImageView 或 View 选项卡,并将宽度设置为 0dp,高度设置为 match_parent,最重要的是 weight 设置为 1(不包括 dp 或任何其他单位)在 LinearLayout 内部查看。这样,选项卡将具有相同的宽度,并且它们将保持在屏幕边界内。

希望这会有所帮助。

【讨论】:

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