2009-11-02 50 views
54

我有一個相當複雜的ListView,具有可變列表項高度。在某些情況下,我需要在列表項中顯示一個額外的視圖,默認情況下隱藏(View.GONE)。通過啓用它(View.VISIBLE),列表項的高度增加(或至少應該是)。爲什麼ListView項不能增長以包裝其內容?

問題: 即使我將項目的根佈局聲明爲wrap_content,並將項目中的每個組件都填充爲fill_parent,但我隱藏/顯示應該更改項目高度的視圖在底部會被簡單切掉其父(項目佈局)的高度增長以充分顯示它。

是否有任何有關ListViews和項目佈局和項目高度,我可能已經錯過了陷阱?

一些更多的意見:

出於測試目的,我現在已經減少了列表項的佈局只包含根的LinearLayout和ImageView的。當我將LinearLayout高度設置爲例如200dip和ImageView fill_parent,我會期望ImageView增長,直到它達到其父級設置的200dip限制。

但是,圖像將只會與其位圖資源一樣高(如果我已將它設置爲wrap_content),並且整個列表項將具有相同的高度(即,如果我已將其設置爲wrap_content也是)。

然而,如果我將圖像高度設置爲例如200dip,那麼列表項的高度會增加,項目佈局也會增加。

換句話說,列表項佈局的layout_height被完全忽略,ImageView上的任何高度值都不是硬編碼的像素值。

+2

HI .. Matthias 請給我一個工作示例。我也面臨同樣的問題,但我不清楚你是如何解決的。問題。 – 2011-02-21 10:36:12

回答

54

我設法解決這個問題,但我不明白爲什麼

正如我所提到的,我已將列表項佈局的layout_height設置爲wrap_content(因爲fill_parent在這裏沒有意義,因爲ListView無限高)。

但是,我已經將中的所有視圖layout_height設置爲,其佈局爲fill_parent。相反,將問題設置爲wrap_content時問題消失。

這就提出了另外兩個問題:

1)什麼是要求fill_parent視圖的語義,當父wraps_content?哪個大小的請求優先?

2)如果fill_parent顯然不起作用,我會如何使視圖填充列表項?

感謝您的輸入人。

+0

Thx那個Matthias =)我有exakt同樣的問題。我也將layout_height設置爲「fill_parent」。我認爲這很明顯。但是,正確的是,當設置爲「wrap_content」時,它解決了問題。 奇怪... – Ted 2010-01-06 03:17:50

+1

謝謝!這是在殺我。 – 2011-05-19 02:24:24

+0

好的,出於某種原因,我有類似的問題,但是當我在列表中只有一個項目時,那麼整個列表視圖對於第一個顯示來說太小了。所以當我增加項目的大小時,視圖不會增長。我必須將視圖設置爲「fill_parent」才能工作。現在,我需要看看發生了什麼事情,我有負載的項目。 – Mortimer 2012-02-01 15:42:12

21

你怎麼膨脹你的行?

如果你不使用它的權利,請嘗試使用LayoutInflater#inflate(layoutId, parent, false)(其中parent提供給getView()newView()AdapterView):

v = getLayoutInflater().inflate(R.layout.list_item, parent, false); 
+0

嗨馬克,提供一個根視圖inflate(),但將attachToRoot設置爲false的語義是什麼?如果相關視圖未附加到rootView,那麼該視圖還有哪些其他的工作? – Matthias 2009-11-04 14:35:54

+0

對不起,我忘了提及:我爲rootView傳遞null,爲attachToRoot傳遞false。畢竟,視圖是由adapter.getView()返回的,我期望將其添加到視圖樹中。 – Matthias 2009-11-04 14:45:02

+1

從文檔中引用「如果爲false,則root僅用於爲XML中的根視圖創建LayoutParams的正確子類」。否則,Android不知道如何將孩子附着到父母身上,所以我認爲它使用了非常通用的LayoutParams。 – CommonsWare 2009-11-04 14:45:51

1

我使用「AbsListView.LayoutParams」在「Adapter.getView()」中手動配置寬度和高度。

14

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

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

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

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

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

27

試試這個: http://www.java2s.com/Code/Android/UI/setListViewHeightBasedOnChildren.htm

public class Utils { 

    public static void setListViewHeightBasedOnChildren(ListView listView) { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) { 
      // pre-condition 
      return; 
     } 

     int totalHeight = 0; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      View listItem = listAdapter.getView(i, null, listView); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    }  
} 
+1

歡迎來到Stack Overflow!雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的重要部分,並提供供參考的鏈接。 – NullUserException 2011-12-06 23:07:27

+1

你在哪裏運行這個方法,以便在重新測量孩子之後? – 2012-09-07 17:46:03

+2

不錯..但是你需要這個讓'params.height'完成一個'px'的值而不是'dp'的值..'int dpValue = totalHeight +(listView.getDividerHeight()*(listAdapter.getCount() - 1)); // dp值 float density = context.getResources()。getDisplayMetrics()。density; params.height =(int)Math.ceil(dpValue * density); //像素值 – 2013-01-04 16:54:22

1

如果你是在爲被調整列表項使用自定義View,那麼你可以從PeBek的答案,這樣當您構建View

addOnLayoutChangeListener(
      new OnLayoutChangeListener() { 
       @Override 
       public void onLayoutChange(View _, int __, int ___, int ____, int bottom, int _____, int ______, 
              int _______, int old_bottom) { 
        final ListView list_view = (ListView) getParent(); 
        final ViewGroup.LayoutParams params = list_view.getLayoutParams(); 
        params.height += bottom - old_bottom; 
        list_view.setLayoutParams(params); 
        list_view.requestLayout(); 
       } 
      }); 

View獲得調整大小時,此回調方法將運行並更新ListView高度以包含高度變化(delta botto米)。其他的值,如果你是view(參照視圖),lefttoprightbottomold_leftold_topold_rightold_bottom他們證明是有用的。它適用於展開和摺疊視圖。

API 11OnLayoutChangeListener所必需的,不知道是否有向後兼容的方式這樣做。

+0

注意:addOnLayoutChangeListener()需要api level 11 – larham1 2013-01-10 00:02:20

+0

@ larham1指出,當我寫這個時,我必須忽略它。 – 2013-01-10 00:06:24

0

我有同樣的問題:我想在我的活動中有一個列表,填充所有的屏幕,當設備是垂直比水平方向。 我解決使用人線性佈局有高度有FILL_PARENT問題,在項目設置的高度在列表中0dp而layout_weight是二傳手爲1

android:layout_weight="1" 
android:layout_height="0dp" 
0

這對我的作品

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:padding="@dimen/activity_vertical_margin"> 

    <TextView 
     android:id="@+id/tv_status" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textSize="17sp" 
     android:text="@string/text_list_devices" /> 

    <ListView 
     android:id="@+id/lv_paired" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:cacheColorHint="#00000000" 
     android:layout_above="@+id/signup_t" 
     android:layout_below="@id/tv_status" 
     /> 

    <Button 

     android:id="@+id/signup_t" 
     style="?android:textAppearanceSmall" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:textColor="@android:color/white" 
     android:layout_alignParentBottom="true" 
     android:text="Print All Records" 
     android:typeface="sans" 
     android:layout_marginLeft="45dp" 
     android:layout_marginRight="45dp" 
     android:textSize="24sp" 
     android:background="@drawable/selector_for_button" 
     android:textStyle="bold" /> 
</RelativeLayout> 
相關問題