如何在自定义Android对话框中排列intger输出 [英] How to line up intger output in custom Android dialog

查看:153
本文介绍了如何在自定义Android对话框中排列intger输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SimpleCursorAdaptor从SQLite数据库显示整数数据。
一切都显示出来,但是对齐方式都是错误的:

对话框如下所示:

 <?xml version = 1.0encoding =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 =OKandroid:layout_below =@ id / lvwScores>
< / Button>
< / RelativeLayout>

使用行xml文件:

 <?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parentandroid:layout_height =fill_parent>
< TableLayout android:layout_width =fill_parentandroid:layout_height =wrap_contentandroid:stretchColumns =0,1,2,3>
< TableRow>
< TextView android:id =@ + id / tvwPlayer1Scorexmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content android:layout_height =wrap_contentandroid:gravity =right/>
< TextView android:id =@ + id / tvwPlayer2Scorexmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content android:layout_height =wrap_contentandroid:gravity =right/>
< TextView Android:id =@ + id / tvwPlayer3Scorexmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content android:layout_height =wrap_contentandroid:gravity =right/>
< TextView android:id =@ + id / tvwPlayer4Scorexmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content android:layout_height =wrap_contentandroid:gravity =right/>
< / TableRow>
< / TableLayout>
< / RelativeLayout>


解决方案

TableLayout 是一个不好的选择。固有的流动性将导致列的宽度根据其内部的内容而变化(尽管拉伸确实最小化了一些),您无法控制(参见下文)。此外,命名空间声明只需要在XML的根元素上,而不是每个元素;)



通过使用这种方式来简化您的行布局:

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =wrap_content
android:orientation =horizo​​ntal>
< 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>

layout_width =fill_parent和每个元素上的 layout_weight =1可以让系统布置所有四个元素,等距间隔(因为它们具有相同的权重和)来填充行。我几乎总是尽可能地嵌套 LinearLayout 代替 TableLayout (这些都是 TableLayout 真的是无论如何)。



从您发布的行XML的另一件事情:将列表项的布局的根元素设置为 layout_height = fill_parent 像你在 RelativeLayout 标签中一样。根据布局管理器的具体情况,布局管理器实际上可能会听你的意见,一行可能会导致整个窗口的出现!



注意关于TABLELAYOUT PARAMS:



如果您坚持使用 TableLayout ,请知道您可以(并且应该)省略所有的来自 TableLayout TableRow的每个孩子的layout_width layout_height 。这两个小部件忽略你所说的内容,并将他们的孩子的值分别设置为MATCH_PARENT和WRAP_CONTENT(分别),所以将它们添加到你的代码中只会在你认为它们应该生效的时候混淆你。



希望帮助!


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

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