Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

android - How to set the ListView Rows Height

I'm showing you the image below in which the height of the rows are not same i want the Rows should be of the same height.

enter image description here

In my program i'm getting the data from the XML service which i parsered and displaying the result on the list view but the height of the ListView Rows are not same.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@drawable/list_selector"
  android:padding="3dip"
  android:layout_width="match_parent"
  android:gravity="center"
  android:layout_height="10dp">

    <TextView android:layout_height="wrap_content" android:text="ITEM"
        android:layout_width="wrap_content" android:id="@+id/txtItem"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15dip"
        android:textStyle="bold"
        android:textColor="#040404"></TextView>

    <TextView android:layout_height="wrap_content" android:text="MANUFACTURER"
        android:layout_width="wrap_content" android:id="@+id/txtItemTwo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dip"
        android:textColor="#FF7F50"

        ></TextView>


</LinearLayout>

I'm already sets the height

android:layout_height="10dp"

but still not same when diplaying on listView.

This is my custom adapter for getVieew :-

 public class MyEventAdapter extends BaseAdapter {

     ArrayList < String > listTitle;
     ArrayList < String > listFullText;

     Activity activity;

     public MyEventAdapter(Activity activity, ArrayList < String > listTitle, ArrayList < String > listFullText) {
         super();
         this.listTitle = listTitle;
         this.listFullText = listFullText;

         this.activity = activity;
     }

     public int getCount() {
         // TODO Auto-generated method stub
         return listTitle.size();
     }

     public Object getItem(int position) {
         // TODO Auto-generated method stub
         return null;
     }

     public long getItemId(int position) {
         // TODO Auto-generated method stub
         return 0;
     }

     private class ViewHolder {
         TextView txtViewTitle;
         TextView txtViewTitleTwo;

     }

     public View getView(int position, View view, ViewGroup parent) {
         // TODO Auto-generated method stub

         ViewHolder title;
         LayoutInflater inflater = activity.getLayoutInflater();

         if (view == null) {
             view = inflater.inflate(R.layout.lview_row, null);
             title = new ViewHolder();

             title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
             title.txtViewTitleTwo = (TextView) view.findViewById(R.id.txtItemTwo);

             view.setTag(title);
         } else {
             title = (ViewHolder) view.getTag();
         }

         title.txtViewTitle.setText(listTitle.get(position));
         title.txtViewTitleTwo.setText(listFullText.get(position));

         return view;
     }
 }

Anyone please me the proper way to getting the desired out result.

Thanks in advance.

question from:https://stackoverflow.com/questions/10379727/how-to-set-the-listview-rows-height

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the problem is that you are mis-using the inflator, don't give null as the root node

if you pass null as the root node, all the size parameters you set in the xml fail to work

the correct code is

view = inflater.inflate(R.layout.lview_row, parent, false);

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...