2012-02-24 34 views
16

điều này phải là một việc rất dễ thực hiện, nhưng có vẻ như nó rất phức tạp. Trong mã của tôi, tôi có ListView này:Đặt chiều cao mục ListView

<ListView android:id="@+id/sums_list" android:layout_width="fill_parent" 
     android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="0dp"></ListView> 

Đó là dân cư với một ArrayAdapter có sử dụng quan điểm này:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="55dp"> 
... 
</LinearLayout> 

Tại sao không phải là mục 55dp cao?

Cảm ơn mọi người

+0

Tại sao 'android: layout_height = "0dp" '? –

+1

Điều gì đang thực sự xảy ra? Bạn có nhìn thấy các mặt hàng nhưng với chiều cao sai? Bạn không thấy mặt hàng nào? Không có bất cứ điều gì rõ ràng là sai với những gì bạn đã đăng, nhưng nhìn thấy bộ chuyển đổi của bạn và biết những gì bạn đang thấy bây giờ sẽ là hữu ích. –

+0

@Secator 0dp xác định liệu trọng số sẽ được áp dụng cho chiều cao hay chiều rộng. vì vậy trong ví dụ này, trọng số của 1 sẽ được áp dụng cho chiều cao như được đặt thành 0dp. –

Trả lời

61

Bạn tăng lượt xem như thế nào?

Nếu bạn đang đặt tham số 'parent' thành null như dưới đây, thì thông số bố cục sẽ bị bỏ qua.

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    View v = inflater.inflate(R.layout.filtered_trip_row, null); 
    … 

    return v; 
} 

Vượt qua 'cha mẹ' để tăng cao sẽ hoạt động như bạn mong đợi.

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    View v = inflater.inflate(R.layout.filtered_trip_row, parent); 
    … 

    return v; 
} 

này giải thích nó một cách chi tiết: Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?

Để tránh bị UnsupportedOperationException, bạn có thể thêm false như một tham số đầu vào cho các inflator. Ví dụ:

inflater.inflate(R.layout.filtered_trip_row, parent, false) 
+0

Cảm ơn Amol, tôi không thể hiểu được LayoutInflater, bây giờ tất cả đều rõ ràng. Tôi có thể làm cho nó hoạt động và trên hết, tôi hiểu tại sao nó lại hoạt động như thế. Cảm ơn rất nhiều – user449689

+1

nếu bạn nhận được hoạt động không được hỗ trợ: hãy kiểm tra điều này: LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (mDisplayWidth/3, 30); \t \t \t convertView.setLayoutParams (params); – cV2

+11

@Amol Brid, tôi gặp lỗi 'java.lang.UnsupportedOperationException: addView (View, LayoutParams) không được hỗ trợ trong AdapterView ' –

9
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    View v = inflater.inflate(R.layout.filtered_trip_row, parent,false); 
    … 

    return v; 
} 

chỉ cần vượt qua giá trị boolean để phương pháp này để nó thêm yourrowlayout để listview và nó sẽ không cung cấp thêm tác lỗi như java.lang.UnsupportedOperationException lỗi: addView (Xem, LayoutParams) không được hỗ trợ trong AdapterView

và thêm yourLayout mà u muốn cư thông qua Adaptor

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="55dp"> 
... 
</LinearLayout> 

nó làm việc cho tôi và tôi thi nk nó có thể giúp bạn

+1

Điều này hoạt động tốt hơn câu trả lời được chấp nhận. Cảm ơn! – Sunkas

0
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:minHeight="60dp" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:weightSum="1" 
    android:background="@color/main_color"> 

    <ImageView 
     android:id="@+id/img_left_menu" 
     android:layout_weight="0.4" 
     android:focusable="false" 
     android:maxHeight="50dp" 
     android:maxWidth="50dp" 
     android:minHeight="50dp" 
     android:minWidth="50dp" 
     android:layout_width="0dp" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/tx_left_menu" 
     android:layout_width="0dp" 
     android:textSize="18dp" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="0.6" 
     android:singleLine="true" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     /> 
</LinearLayout> 

trong adapter thiết phụ huynh như mô tả ở trên

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    View v = inflater.inflate(R.layout.filtered_trip_row, parent,false); 
    … 

    return v; 
} 
Các vấn đề liên quan