2013-11-14 45 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"