2017-07-07 74 views
0

首先,我是新來的機器人,所以如果我錯過了一些基本的道歉。Android的ListView寬度不斷變化

我有一個頁面有兩個並排的ListViews,兩個都有不同數量的內容。我也有ListView上方的TextView和列表視圖下方的另一個TextView。這些文本視圖框基於在兩個ListView中選擇的項目而改變。

這兩個ListView並排坐在一起,佔用每個屏幕的一半,而Textview直接位於上方和正下方,均位於頁面中央。圖像如下所示。

This is the page looking normal on load.

的問題是,當我從任一列表中選擇一個項目。我有一種感覺我錯過了一些XML屬性,但我不確定哪些屬性或者甚至是這種情況。當選擇一個項目時,讓我們從右側的ListView中看到,底部的TextView更新了從數組中取得的文本。 ListView也決定改變寬度,我不知道爲什麼這是....我不希望ListView改變寬度。我希望它只佔用頁面的一半和頁面的一半。

This is the page after an item from the right ListView has been selected.

我也想保持對事物的RelativeLayout的。我也相信這只是一個XML問題,不適用於適配器或任何其他代碼,所以我現在不會包含它。如果需要,我可以包含它。

任何幫助將是偉大的。提前致謝。

content_titles.xml我的活動XML文件

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".TitlesActivity"> 

<ListView 
    android:id="@+id/unlocked_titles_list" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:longClickable="true" 
    android:layout_below="@+id/current_title" 
    android:layout_alignEnd="@+id/requirements" 
    android:layout_marginEnd="26dp" 
    android:layout_above="@+id/requirements"> 
</ListView> 

<TextView 
    android:id="@+id/current_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="65dp" 
    android:text="Current Title: Novice" 
    android:textSize="24sp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"/> 

<ListView 
    android:id="@+id/locked_titles_list" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignStart="@+id/requirements" 
    android:layout_marginStart="28dp" 
    android:layout_above="@+id/requirements" 
    android:layout_below="@+id/current_title"/> 

<TextView 
    android:id="@+id/requirements" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="34dp" 
    android:text="temp" 
    android:textAlignment="center" 
    android:textSize="24sp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

activity_listview.xml用作列表視圖

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dip" 
     android:textSize="16dip" 
     android:textStyle="bold" > 
</TextView> 

回答

0

的各行你有你的layout_width屬性設置爲wrap_content。這意味着它們可以隨着數據的變化而改變。我建議您將ListView設置爲LinearLayoutorientation:horizontal,並設置每個元素佔用的空間量爲layout_weight。這是一個有關SO質疑What does android:layout_weight mean?

+0

我真的想保持在RelativeLayout中。有另一種方法嗎? –

4

的問題與佈局可以通過滾動型引起的是包裝

http://developer.android.com/reference/android/widget/ExpandableListView.html

偶然發現了一些注意事項」 ......注意:您不能使用值如果父級的大小也沒有嚴格指定,那麼在XML中的ExpandableListView的android:layout_height屬性的wrap_content(例如,如果父級是ScrollView,您無法指定wrap_content,因爲它也可以是任意長度)。但是,可以使用wrap_content if ExpandableListView父級具有特定的大小,例如100像素。「

我刪除了包裝ScrollView和線性佈局開始正常工作。現在只有瞭解如何將這些東西包裝到ScrollView中。上帝幫助我

但無論如何這是非常奇怪的行爲。我認爲fill_parent不是真的正確的措辭。當使用heirarchyviewer工具時,我總是看到layout_width和leayout_height的WRAP_CONTENT和MATCH_PARENT值。所以可能fill_parent實際上意味着match_parent,這使我處於認知失調狀態。