Single row to fill_parent ListView height

大憨熊 提交于 2019-12-01 21:23:46

问题


There's one row in the listView which I want to be with the same height as the listView (let's say that is full-screen).

The row layout looks like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/error" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:minHeight="30dip"
        android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>

And the adapter's getView is

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View row = inflater.inflate(R.layout.myrow, parent, false);
   return row;
}

Bu the row is displayed the same as it would be with android:layout_height="wrap_content".

Layout preview shows the row filling it's parent and I'm using inflate(R.layout.myrow, parent, false);, the listView is certainly displayed full-screen and the row is only as tall as the image + textView.

Am i missing something important ?


回答1:


I had similar problem I think I have solved so reporting here.

I have more rows in my ListView, but I want that the row height be the same of the listview, which in turn shall occupy all the remaining space of the layout. My row is made of just one ImageView. I had problems with the following:

-I want that smaller images expand to occupy all the space -Bigger images shall not exapnd more than a row height

If I have made any error please leave a note here.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:gravity="center"
    android:orientation="vertical">
    <ListView 
        android:id="@id/lay_main_firma_objekti"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    </ListView>      
</LinearLayout>   

Row layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center">       
  <TextView
      android:id="@+id/xx"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>  
    <ImageView              
        android:id="@id/imageView_obj"
        android:contentDescription="@string/objektDesc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@id/xx"
        android:layout_alignTop="@id/xx"
        android:layout_alignRight="@id/xx"
        android:layout_alignBottom="@id/xx"
        android:scaleType="fitXY"
        android:src="@drawable/custom_prazno" />                    
</RelativeLayout>

Then in the Adapter getView it is important to

convertView.setMinimumHeight(parent.getMeasuredHeight());
ImageView slika=(ImageView)v.findViewById(R.id.imageView_obj);    
slika.setMinimumHeight(parent.getMeasuredHeight());
TextView xx=(TextView)v.findViewById(R.id.xx);
xx.setHeight(parent.getMeasuredHeight());

I think that is. hth




回答2:


just wondering if there's a particular reason why you want to have a ListView containing only one row? As opposed to just using the RelativeLayout directly instead of ListView?




回答3:


I've ended up with hardcoding the row height. There is a problem with relative layout after setting it's minimumHeight so I've also replaced it with the LinearLayout with centered gravity.

Please let me know if there's a better solution letting Android to do it's job and minimizing hardcode.



来源:https://stackoverflow.com/questions/13070361/single-row-to-fill-parent-listview-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!