2011-10-08 103 views
0

我正在開發Sudoku遊戲,並且遇到了textViews高度的問題。和任何Sudoku一樣,用戶可以選擇輸入一個數字或者提示一些提示。數字與Fontsize 14一起工作良好,但是當我將提示字體大小更改爲7(dip或sp)時,儘管周圍有很多空間,但textView高度增加了(對於字體大小6甚至1行文本)。無論文本大小如何修復textView高度。降低字體大小時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> 
+0

你能後的你有大小14和大小7字體看到的截圖? – slayton

+0

@slayton:對不起,我是stackoverflow的新用戶,我目前尚未發佈圖片。但是,如果我更改了textSize(在運行時或在佈局查看器中),則整行的高度會增加。 –

+0

考慮使用gridview而不是tableview – slayton

回答

0

我找到了解決我的問題,而無需重做我的代碼&設計與GridView控件(感謝斯雷頓的建議,但我發現一個更容易的解決辦法)。

我不完全理解爲什麼,但是在使用textviews的屬性後,我發現當我將一個textview的textsize更改爲7(或其他任何實際值)時,我需要將其layoutheight更改爲Fill_Parent(但使用layoutheight Wrap_Content將所有textView保留在大小爲14的行中。我以編程方式執行此操作。新的佈局是

<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> 

我使用代碼來改變大小&的layoutheight在一起:

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));