具有自定义视图的 ActionBar 选项卡未居中 [英] ActionBar Tab with custom View not centered

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

问题描述

我需要为我的标签使用自定义视图,问题是 fill_parent 不起作用(如 这里).

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 没用,第二个会在 tab 的中心.

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.

这是我使用的.我只想要一个文本视图,右上方有另一个文本视图.

Here is what I used. I just wanted 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>

这适用于垂直对齐,但视图只有我放入文本视图的内容的宽度.所以如果你需要使用完整的宽度,你应该在其中一个不可见的文本视图中放一个非常长的单词.

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.

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

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