Android面试基础Android开发Android

解决ListView中Item布局设置的layout_heigh

2016-03-19  本文已影响1793人  我是吸血鬼

大家肯定遇到过在ListView的item布局中设置的高度没有效果的问题。
出现这种情况的代码示例:

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"    
     android:layout_width="match_parent"       
     android:layout_height="100dip" 
     android:gravity="center_vertical" 
     android:orientation="horizontal">
    <TextView 
       android:layout_width="wrap_content"                             
       android:layout_height="wrap_content" 
       android:text="test" />       
</Linearlayout>  

public View getView(int position, View convertView, ViewGroup parent) {   
    if (convertView == null) {  
         convertView = inflate(R.layout.item_lv_test, null); 
    } 
    return convertView;
 }  

如果用上面的代码会发现设置100dp是无效的。
而如果换成下面的代码就可以了:

   public View getView(int position, View convertView,  ViewGroup  parent) {
     if (convertView == null) {
            convertView =  inflate(R.layout.item_lv_test, parent,false); 
     } 
       return convertView;
  }  
上一篇下一篇

猜你喜欢

热点阅读