如何在没有图像的情况下指定自定义标签视图?

时间:2016-03-09 17:16:39

标签: android android-layout

我已经创建了一个布局XML,并按照正确的顺序调用setCustomTabView,并验证它与此处描述的几乎相同。

How can custom tab view be implemented using setCustomTabView method in SlidingTabColor sample?

我不想要一张照片。我只想控制文字大小。我想在小型手机上使用默认设置,但平板电脑的尺寸是原来的两倍。平板电脑上的默认值非常小。该应用程序在户外使用,可见性和大按钮很重要。我有两个"水桶"至今。以下示例是默认示例。

如果我使用这个xml布局,我只看到第一个标签的文本,而没有颜色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Default sized TextView for tab title.
      This allows us to specify a larger one for tablets in layout-w720dp. -->
    <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="18dp"/>
</LinearLayout>

Custom Tab Layout - text only

如果我为图像添加xml并提供源,它就可以工作。如果我省略了源代码,结果与只有TextView的布局相同。

     <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="18dp"/>
    <ImageView
        android:layout_width="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_height="wrap_content" />

With an (unwanted) image

在我的一个实验中,我增加了文字大小(非常大)而没有图像我得到了所有四个标题。

如何仅使用TextView复制默认选项卡标题? (我有一个可行的解决方案,我在运行时检测到指标并将文本大小加倍,但我希望通过不同的布局子文件夹来控制它。)

我看到默认的tabView是一个简单的,实例化的TextView,而tabTitleView等同于它。如果我可以在布局中的TextView上执行findViewById,我也可以这样做。但尝试这样做会导致TextView为空 - 可能是因为尚未建立ViewGroup。但是如果我使用我的布局,孩子已经有了父母,并且不能在标签条的addView()中使用。

1 个答案:

答案 0 :(得分:0)

这很有用。

<TextView
    android:id="@+id/tab_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="12sp"
    android:gravity="center_vertical|center_horizontal"
    android:padding="16dp" />

使用调试器(并查看创建默认tabView的源)我看到我的TextView的重力为0x800033,而他们的重力为0x000011。更改android:gravity并添加与默认固定相同的填充。我认为center也可行。它是垂直|水平的别名(1&lt;&lt;&lt;&lt; 4&lt;&lt;&lt; 0)。

我还在setAllCaps(true)上调用tabTitleView以保持一致性。