如何排队在定制的Android对话框intger输出 [英] How to line up intger output in custom Android dialog
问题描述
我从SQLite数据库使用SimpleCursorAdaptor显示整数数据。
一切都显示出来,但调整是完全错误的:
该对话框看起来是这样的:
<?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> < ListView的机器人:ID =@ + ID / lvwScores
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<按钮
机器人:ID =@ + ID / btnOK
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文字=OK机器人:layout_below =@ ID / lvwScores>
< /按钮>
< / RelativeLayout的>
通过该行的XML文件:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
< TableLayout的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:stretchColumns =0,1,2,3>
<&的TableRow GT;
< TextView的机器人:ID =@ + ID / tvwPlayer1Score的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:比重=正确的/>
< TextView的机器人:ID =@ + ID / tvwPlayer2Score的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:比重=正确的/>
< TextView的机器人:ID =@ + ID / tvwPlayer3Score的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:比重=正确的/>
< TextView的机器人:ID =@ + ID / tvwPlayer4Score的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:比重=正确的/>
< /&的TableRow GT;
< / TableLayout>
< / RelativeLayout的>
TableLayout
是一个不错的选择。它的固有流动性将导致列在宽度基于它们的内部的内容变化(虽然拉伸确实减少一些这),您没有控制权(见下文)。此外,该命名空间声明只需要是XML的根元素,不是每一个上;)
简化您的排布置急剧用这个代替:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< TextView的机器人:ID =@ + ID / tvwPlayer1Score
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=右/>
< TextView的机器人:ID =@ + ID / tvwPlayer2Score
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=右/>
< TextView的机器人:ID =@ + ID / tvwPlayer3Score
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=右/>
< TextView的机器人:ID =@ + ID / tvwPlayer4Score
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=右/>
< / LinearLayout中>
每个元素 layout_width =FILL_PARENT
和 layout_weight =1
组合指示系统铺陈所有四个元件,相等间隔(因为它们具有相同的权重总和)来填充的行。我几乎总是使用嵌套的的LinearLayout
代替 TableLayout
尽可能(这是所有 TableLayout
真的是这样)。
从您发布的行XML另一件事:这不是设置列表项目的布局与 layout_height = FILL_PARENT
根元素就像你在有一个好主意 RelativeLayout的
标记。具体情况取决于该布局获取的取材,布局管理器实际上可能听你的话,一个行可能最终以整个窗口!
请注意关于TABLELAYOUT PARAMS:
如果你坚持用 TableLayout
坚持,知道你可以(也应该)忽略所有的 layout_width
和 layout_height
从 TableLayout
和的TableRow
的每一个孩子属性。这两个部件不理你说的,并设置自己的孩子的价值观MATCH_PARENT什么,WRAP_CONTENT(分别),所以把它们添加到您的code只会混淆你,如果你觉得他们应该生效。
希望帮助!
I am displaying integer data from an SQLite database using a SimpleCursorAdaptor. Everything shows up but the alignment is all wrong:
The dialog looks like this:
<?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">
<ListView android:id="@+id/lvwScores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnOK "
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK" android:layout_below="@id/lvwScores">
</Button>
</RelativeLayout>
With the row xml file:
<?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">
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0,1,2,3">
<TableRow >
<TextView android:id="@+id/tvwPlayer1Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer2Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer3Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer4Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
</TableRow>
</TableLayout>
</RelativeLayout>
TableLayout
is a bad choice. It's inherent fluidity will cause the columns to vary in width based on the content inside of them (although the stretching does minimize some of this), which you have no control over (see below). Also, the namespace declaration only needs to be on the root element of the XML, not each one ;)
Simplify your row layout drastically by using this instead:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/tvwPlayer1Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer2Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer3Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="@+id/tvwPlayer4Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
The combination of layout_width="fill_parent"
and layout_weight="1"
on each element tells the system to lay out all four elements, equally spaced (since they have the same weight sum) to fill the row. I almost always use nested LinearLayout
in place of TableLayout
whenever possible (that's all TableLayout
really is anyway).
Another thing from the row XML you posted: it's not a good idea to set the root element of a list item's layout with layout_height=fill_parent
like you have in the RelativeLayout
tag. Depending on where this layout get's drawn, the layout manager might actually listen to you and one row might end up taking the entire window!
NOTE ABOUT TABLELAYOUT PARAMS:
If you insist on sticking with TableLayout
, know that you can (and should) omit all the layout_width
and layout_height
attributes from every child of TableLayout
and TableRow
. Those two widgets ignore what you say and set the values of their children to MATCH_PARENT and WRAP_CONTENT (respectively), so adding them to your code will only serve to confuse you if you think they're supposed to take effect.
Hope that Helps!
这篇关于如何排队在定制的Android对话框intger输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!