Android Inside Custom ListView, TextView Weight pr

2019-09-11 06:48发布

问题:

I have a custom listView. The listview data are setting by some textview horizontally for each rows. In main layout I have same number of textView for setting the title of those columns. My problem is, the main layout working fine with the weight property. But the custom value setting layout for listview which contains the textview is not working with the weight property.

Main Layout : (Weight property working correctly)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
         android:weightSum="8">
        <TextView
            android:layout_weight="0"
            android:id="@+id/cid"
            android:layout_width="0dp"
            android:textSize="16dp"
            android:text="B.Id" />
        <TextView
            android:layout_weight="1"
            android:id="@+id/nm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Name" />      
        <TextView
            android:layout_weight=".9"
            android:id="@+id/mob"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Mobile" />        
        <TextView
            android:layout_weight="1.2"
            android:id="@+id/eml"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Email" />     
        <TextView
            android:layout_weight=".8"
            android:id="@+id/bk_no"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Bike No" />       
        <TextView
            android:layout_weight=".4"
            android:id="@+id/dy_rt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Rate" />      
        <TextView
            android:layout_weight=".9"
            android:id="@+id/frm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="From Date" />     
        <TextView
            android:layout_weight=".9"
            android:id="@+id/to"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="To Date" />       
        <TextView
            android:layout_weight=".25"
            android:id="@+id/dy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Dys" />       
        <TextView
            android:layout_weight=".45"
            android:id="@+id/cst"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />     
        <TextView
            android:layout_weight="0"
            android:id="@+id/vid"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="V.ID" />      
        <TextView
            android:layout_weight=".9"
            android:id="@+id/book_dt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="1dp"
            android:text="Bk Date" />
        <TextView
            android:layout_weight=".3"
            android:id="@+id/rting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Rating" />
    </LinearLayout>
    <ListView 
        android:id="@+id/list_past_report"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>

Custom ListView Value setting Layout:(report_custom_list)(Weight property not working)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout 
    android:id="@+id/cus_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:weightSum="8" >
<TextView
    android:layout_weight="0"
    android:id="@+id/id"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight="1"
    android:id="@+id/nm"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".9"
    android:id="@+id/mob"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight="1.2"
    android:id="@+id/eml"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".8"
    android:id="@+id/bk_no"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".4"
    android:id="@+id/dy_rt"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".9"
    android:id="@+id/frm"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".9"
    android:id="@+id/to"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".25"
    android:id="@+id/dy"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".45"
    android:id="@+id/cst"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight="0"
    android:id="@+id/vid"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight=".9"
    android:id="@+id/book_dt"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />
<TextView
    android:layout_weight=".3"
    android:id="@+id/rting"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

</LinearLayout>
</LinearLayout>

Custom Adapter Setting values:

 @Override
public View getView(final int position, View convertView, ViewGroup parent)     {
    final Object[] id_obj=_id.toArray();
    Object[] name_obj=c_name1.toArray();
    Object[] mb_obj=m_no1.toArray();
    Object[] eml_obj=email1.toArray();
    ...
    Holder holder=new Holder();
    View rowView;       
         rowView = inflater.inflate(R.layout.report_custom_list, null);
         if(position%2==0)
             rowView.setBackgroundColor(Color.YELLOW);
         else
             rowView.setBackgroundColor(Color.GRAY);
         holder.tv=(TextView) rowView.findViewById(R.id.id);
         holder.tv1=(TextView) rowView.findViewById(R.id.nm);
         holder.tv2=(TextView) rowView.findViewById(R.id.mob);
         ...

         holder.tv.setText((CharSequence) id_obj[position]);
         holder.tv1.setText((CharSequence) name_obj[position]);
         holder.tv2.setText((CharSequence) mb_obj[position]);
         ...
     rowView.setOnClickListener(new OnClickListener() {            
     @Override
     public void onClick(View v) {
     }
     });

    return rowView;
 }

Before I hard coded the width for the textview. But if it goes to different screen the design collapse. How to fix this?

回答1:

Probably the problem is: ​​

<ListView 
    android:id="@+id/list_past_report"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ListView>`

Replace layout_width="wrap_content" with layout_width="match_parent"



回答2:

change listviews layout_width to fill_parent

android:layout_width="fill_parent"


回答3:

I think you have to update you main layout file with this

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

<ListView 
    android:id="@+id/list_past_report"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></ListView> </LinearLayout>

and make some changes in Adapter class also

public View getView(final int position, View convertView, ViewGroup parent)     {
final Object[] id_obj=_id.toArray();
Object[] name_obj=c_name1.toArray();
Object[] mb_obj=m_no1.toArray();
Object[] eml_obj=email1.toArray();
...
Holder holder=new Holder();
View rowView;       
     rowView = inflater.inflate(R.layout.report_custom_list, null);
     if(position%2==0)
         rowView.setBackgroundColor(Color.YELLOW);
     else
         rowView.setBackgroundColor(Color.GRAY);
     holder.tv=(TextView) rowView.findViewById(R.id.id);
     holder.tv1=(TextView) rowView.findViewById(R.id.nm);
     holder.tv2=(TextView) rowView.findViewById(R.id.mob);
     ...
     if(position == 0){
       holder.tv.setText("Title1");
       holder.tv.setText("Title2");
       holder.tv.setText("Title3");
       ....
     }
     else{
         holder.tv.setText((CharSequence) id_obj[position]);
         holder.tv1.setText((CharSequence) name_obj[position]);
         holder.tv2.setText((CharSequence) mb_obj[position]);
         ...
     }

 rowView.setOnClickListener(new OnClickListener() {            
 @Override
 public void onClick(View v) {
 }
 });

return rowView;}