2013-11-14 52 views
1

我想排列列表视图中的6个字段我有位于屏幕顶部的6个标签字段。我已将所有文本制作成相同大小,maxEms大小相同,布局权重完全相同。我的问题是,列表视图不会一直与屏幕左侧对齐。它似乎有一些内置的默认左边距。我可以摆脱重量并在我的标签上设置手动边距以使所有内容组合,但我认为这可能是一个噩梦,当不同大小的屏幕发挥作用时。如何在TextViews下正确对齐一个列表视图

下面是对我的屏幕第一布局(在此布局包含代表标签为我ListView中TextViews的线性布局位于一个相对布局的内部)

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="15dp" 
    android:layout_below="@+id/dateDisplayTextView" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="#1" 
     android:textColor="@color/green" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="#2" 
     android:textColor="@color/green" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="#3" 
     android:textColor="@color/green" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="#4" 
     android:textColor="@color/green" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="#5" 
     android:textColor="@color/green" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" 
     android:maxEms="2" 
     android:text="PB" 
     android:textColor="@color/red" 
     android:textSize="16sp" /> 

</LinearLayout> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_above="@+id/emailButton1" 
    android:layout_below="@id/linearLayout1" > 

</ListView> 

<Button 
    android:id="@+id/emailButton2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="Back" /> 

<Button 
    android:id="@+id/emailButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:onClick="emailTickets" 
    android:layout_alignParentRight="true" 
    android:text="Email tickets to contacts" /> 

,这里是是被我的自定义适配器

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:orientation="horizontal" > 


<TextView 
    android:id="@+id/number1TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/number2TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/number3TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/number4TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/number5TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/numberPBTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxEms="2" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

</LinearLayout> 

内扩大,这里的布局是我的布局图像演示运行我的问题

Screen shot

我试图调节我的标签恶魔居住的线性布局的左边界,但它没有影响。我对这篇较长的文章表示歉意,我只是想确保我提供了所有相关信息。在此先感谢

回答

2

的组合:

android:layout_width="wrap_content" 
    android:layout_weight="1" 

手段,该文本字段的内容,比至少大小,无论留下什么空间,均匀分布在田间。拥有不同的内容,会导致文本字段的宽度不同。 android:maxEms不会改变它,因为它定义了最大宽度,该字段的宽度可以更小。

两个可能的定义将导致正确的大小。取为在标题提交的每个文本和代码行:

1设的Android找出正确的宽度:

<TextView 
    android:id="@+id/..." 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:textSize="16sp" 
    android:text="TextView" /> 

设置宽度为0的装置,即所有剩余空间和均匀分配(由layout_weight加权)。

2。经由ems

<TextView 
    android:id="@+id/..." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:ems="2" 
    android:textSize="16sp" 
    android:text="TextView" /> 

ems设置字段的宽度限定的宽度。再次,剩下的空间将被分配。

+0

感谢,起初我没有得到积极的结果,但因为我在我的ArrayAdapter中有一些奇怪的String格式,试图抵消差异。像魅力一样工作。 –

0

可以使用

安卓layout_padding

这可能是对你有用。

0

尝试给您的自定义适配器重力textViews左

android:gravity="left"