减小字体大小时TextView高度增加

时间:2011-10-08 17:03:01

标签: android textview height font-size

我正在开发一个数独游戏,我遇到了textViews的高度问题。与任何数独游戏一样,用户可以选择输入数字或添加一些提示。使用Fontsize 14可以很好地处理数字,但是当我将提示字体大小更改为7(dip或sp)时,尽管周围有很多空间,但textView高度会增加(字体大小为6,甚至1行文本也是如此) )。无论textize如何,我如何修复textView高度。

这是布局部分:

    <TableLayout android:id="@+id/tableLayout1"
    android:layout_marginTop="5dip" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content">
    <TableRow android:layout_width="wrap_content" android:id="@+id/TableRow01" android:layout_height="wrap_content">
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV00" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:text="2"
            android:drawableTop="@drawable/line_h" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV01" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="2" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV02" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV03" android:paddingLeft="1dip" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:text="2"
            android:drawableTop="@drawable/line_h" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV04" android:clickable="true"
            android:height="34dip" android:layout_height="fill_parent"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="2" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV05" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV06" android:paddingLeft="1dip" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:text="2"
            android:drawableTop="@drawable/line_h" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV07" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV08" android:clickable="true"
            android:layout_height="fill_parent" android:height="34dip" android:text="1 2 3"></TextView>
    </TableRow>
    <TableRow android:layout_width="wrap_content" android:id="@+id/TableRow02"
        android:layout_height="wrap_content">
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="32dip" android:text="1"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV10" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:digits="@string/inpt" android:width="30dip"
            android:text="2" android:gravity="center" android:layout_width="fill_parent"
            android:textColor="@android:color/black" android:id="@+id/TV11"
            android:clickable="true" android:layout_height="fill_parent"
            android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="32dip" android:text="2"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV12" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="32dip" android:text="1"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV13" android:paddingLeft="1dip" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:digits="@string/inpt" android:width="30dip"
            android:text="2" android:gravity="center" android:layout_width="fill_parent"
            android:textColor="@android:color/black" android:id="@+id/TV14"
            android:clickable="true" android:layout_height="fill_parent"
            android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="34dip" android:text="2"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV15" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="34dip" android:text="1"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV16" android:paddingLeft="1dip" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:digits="@string/inpt" android:width="30dip"
            android:text="2" android:gravity="center" android:layout_width="fill_parent"
            android:textColor="@android:color/black" android:id="@+id/TV17"
            android:clickable="true" android:layout_height="fill_parent"
            android:height="32dip"></TextView>
        <TextView android:typeface="monospace" android:drawableRight="@drawable/line_v"
            android:digits="@string/inpt" android:width="34dip" android:text="2"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV18" android:clickable="true"
            android:layout_height="fill_parent" android:height="32dip"></TextView>
    </TableRow>

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法而无需重做我的代码和使用gridview进行设计(感谢slayton的建议,但我找到了一个更简单的解决方法)。

我不完全理解为什么但是在使用textviews的属性之后,我发现当我将一个textview的textize更改为7(或实际上任何其他值)时,我需要将其layoutheight更改为Fill_Parent(但是将所有textView保留在大小为14的行中,并使用layoutheight Wrap_Content)。我以编程方式执行此操作。新布局是

    <TableLayout android:id="@+id/tableLayout1"
    android:layout_marginTop="5dip" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content">
    <TableRow android:layout_width="fill_parent" android:id="@+id/TableRow01" android:layout_height="wrap_content">
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV00" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:text="2"
            android:drawableTop="@drawable/line_h" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV01" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="2" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV02" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV03" android:paddingLeft="1dip" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:text="2"
            android:drawableTop="@drawable/line_h" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV04" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="2" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:layout_width="fill_parent" android:textColor="@android:color/black"
            android:id="@+id/TV05" android:clickable="true"
            android:height="34dip" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:text="1" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:gravity="center"
            android:textColor="@android:color/black"
            android:id="@+id/TV06" android:paddingLeft="1dip" android:clickable="true"
            android:height="34dip" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:digits="@string/inpt" android:width="30dip" android:drawableTop="@drawable/line_h" android:textColor="@android:color/black"
            android:id="@+id/TV07" android:clickable="true"
            android:height="34dip" android:layout_width="fill_parent" android:maxHeight="34dip" android:maxWidth="30dip" android:gravity="center" android:text="2" android:layout_height="wrap_content"></TextView>
        <TextView android:typeface="monospace" android:drawableBottom="@drawable/line_h"
            android:drawableRight="@drawable/line_v" android:digits="@string/inpt"
            android:width="34dip" android:drawableTop="@drawable/line_h"
            android:drawableLeft="@drawable/line_v" android:textColor="@android:color/black"
            android:id="@+id/TV08" android:clickable="true"
            android:height="34dip" android:maxHeight="34dip" android:layout_width="fill_parent" android:gravity="center" android:text="2" android:layout_height="wrap_content"></TextView>
    </TableRow>

我使用代码来改变大小&amp;布局高度:

    tv.setTextSize(7);
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    tv.setTextSize(14);
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));