How to show Fixed Number of Rows in a listView

时间秒杀一切 提交于 2019-12-04 12:40:45

Here i am Just Modifying Your Layout just check it out!!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/txtFilename"
        android:layout_width="375dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
<LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="300dip">
    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="2dp"


        /> 


</LinearLayout>
     <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray"
        android:padding="5dp" >

        <Button 
            android:id="@+id/btnOk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK"/>

        <Button 
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CANCEL"/>
    </LinearLayout>  


</LinearLayout>

I have written Demo for this Like:

if you want to fix Height of ListView to 5 items. then you have to set Height of ListView pragmatically by calculating listItems height.

First you need to create Layout for List Item and give it some fixed height, here i'm added height in Tag cause runtime i was getting layouts height as 0.

This is ListItem Layout

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:background="@android:color/black"
    android:tag="40"
    android:layout_height="40dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XXX"
        android:textColor="@android:color/darker_gray"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/text"
        android:text="ZZZ"
        android:textColor="@android:color/darker_gray"
        android:textSize="30sp" />

</RelativeLayout>

and to set Height of ListView:

list    =   (ListView) findViewById(R.id.listView1);

    adapter = new Adapter(MainActivity.this, setter);
    list.setAdapter(adapter);        

    RelativeLayout.LayoutParams lstViewParams = (LayoutParams) list.getLayoutParams();        
    View lisItemInflatedView = View.inflate(this, R.layout.item, null);        
    RelativeLayout relLayoutInLstItem = (RelativeLayout) lisItemInflatedView.findViewById(R.id.relativeLayout);       
    final float scale = this.getResources().getDisplayMetrics().density;
    int pixels = (int) ( Integer.parseInt( relLayoutInLstItem.getTag().toString() ) * scale + 0.5f);    
    int listHeigt = pixels  * 5 + 5;       
    lstViewParams.height = listHeigt;

here's the screenshot..

Hope this will Help.

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