2015-06-25 4 views
0

Я работаю с Android Studio и проектирую макет, как показано ниже. Однако в моем ListView lv_comics отображено достаточно места для 1 элемента. Я попытался установить layout_height для fill_parent, но он все равно не работает. Однако, когда я устанавливаю layout_height в определенный размер, это нормально, но это плохо, когда я переключаюсь на другой размер экрана. Кто-нибудь может мне помочь?Android - Высота ListView просто подходит 1 ListView item

<android.support.v4.widget.SwipeRefreshLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/layout_swiperefresh" 
android:background="@color/grey"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:text="Latest Update" 
       android:textColor="@color/white" 
       android:background="@color/indigo_main" 
       android:textSize="10dp"/> 
      <Gallery 
       android:id="@+id/gallery1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:text="Most Popular" 
       android:textColor="@color/white" 
       android:background="@color/indigo_main" 
       android:textSize="10dp"/> 
      <Gallery 
       android:id="@+id/gallery2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:text="New Manga" 
      android:textColor="@color/white" 
      android:background="@color/indigo_main" 
      android:textSize="10dp"/> 
     <ListView 
       android:drawSelectorOnTop="true" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/lv_comics" /> 

    </LinearLayout> 
</ScrollView> 

Вот мой макет шоу. http://i.stack.imgur.com/KFIJy.jpg

+1

'' ListView' внутри ScrollView' ...? Не хорошая идея. –

+0

Listview внутри scrollview - не очень хорошая идея. Чтобы обойти это, вы можете добавить свои два представления в качестве заголовков в список – Eoin

ответ

0

пытаются это вычислить высоту динамически

public static void setListViewHeight(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) { 
     return; 
    } 
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); 
    int totalHeight = 0; 
    View view = null; 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
     view = listAdapter.getView(i, view, listView); 
     if (i == 0) { 
      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT)); 
     } 
     view.measure(desiredWidth, MeasureSpec.AT_MOST); 
     totalHeight += view.getMeasuredHeight(); 
    } 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
    listView.requestLayout(); 
}