动作条选项卡自定义视图不集中 [英] ActionBar Tab with custom View not centered

查看:152
本文介绍了动作条选项卡自定义视图不集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自定义视图我的标签,问题是FILL_PARENT不起作用(看<一href="http://stackoverflow.com/questions/19754804/android-actionbar-tab-setcustomview-doesnt-fill-parent">here).

I need to use a custom View for my tabs, the problem is that fill_parent doesn't work (as seen here).

所以,我需要用保证金之类的东西,但为了在所有配置中的选项卡内中心的视图(横向/纵向,还是在平板电脑上,其中标签的高度将发生变化),这是一个有点棘手做

So I need to use margin and stuff, but in order to have the view centered inside the tab in all configuration (landscape/portrait, or on a tablet where the height of the tabs will change) it's a bit tricky to do.

我不知道,在每个配置中使用什么样的价值。另外,我没有找到系统用来启动的默认布局。

I don't know what value to use on each configuration. Plus, I don't find the default layout that the system uses to start with.

推荐答案

所以,如果你想要做的,你需要做一点脏东西。 由于FILL_PARENT不工作,你需要强制根认为有比标签更高度高度,然后集中你真正需要的。所以,我的第一个LinearLayout中是没有用的,而第二个将在标签的中心。

So, if you want to do that you will need to do a bit of dirty stuff. Since fill_parent doesn't work, you need to force the root view to have more height than the tab height and then center what you really need. So my first LinearLayout is useless, and the second one will be in the center of the tab.

下面是我用的东西。我只是wanter 1的TextView与另一个在它的右上角。

Here is what I used. I just wanter one textview with another one at the top right of it.

<?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:orientation="vertical">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="invisible">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_tab_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_tab_title"
        android:layout_gravity="right"
        android:background="@drawable/bg_notification"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:text="1"
        android:gravity="center"
        android:minWidth="14dp"
        android:textStyle="bold"
        android:textColor="@color/blanc"
        android:textSize="8dp" />
    <TextView
        android:id="@+id/tv_tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/noire"
        android:textSize="12dp"
        android:textStyle="bold" />
    <TextView //this has the same height as the `tv_tab_count` so it remains centered vertically
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_tab_title"
        android:layout_gravity="right"
        android:background="#0f0"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:text="1"
        android:textSize="8dp"
        android:visibility="invisible" />
</LinearLayout>

这工作的垂​​直对齐方式,但认为只有我把我的TextView内容的宽度。所以,如果你需要使用完整的宽度,你应该把在无形textviews之一真的很长字。

This work for the vertical alignment but the view only has the width of the content I put in my textview. So if you need to use the complete width, you should put a really really long word in one of the invisible textviews.

这篇关于动作条选项卡自定义视图不集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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