2016-02-03 112 views
0

我的ListView我想成爲一個50dp的高度,但問題是無論我設置xml佈局文件的值如何,它都會自動變得更大。 這裏是我的列表視圖佈局:ListView不能正確縮放

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/mainListView" 
    android:layout_below="@+id/" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="22dp" /> 

我rowview是

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

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/" 
      android:layout_alignParentTop="false" 
      android:layout_alignParentLeft="false" 
      android:layout_alignParentStart="false" 
      android:textColor="#000000" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="5dp" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/" 
      android:layout_alignParentRight="false" 
      android:layout_alignParentEnd="false" 
      android:layout_alignParentLeft="false" 
      android:layout_toRightOf="@+id/" 
      android:textColor="#000000" 
      android:layout_centerVertical="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/usernameTextView" 
      android:layout_below="@+id/" 
      android:textColor="#666666" 
      android:layout_toRightOf="@+id/" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/" 
      android:layout_alignParentTop="false" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" /> 

</RelativeLayout> 

而且我LocalQueueAdapter

package .skyrealm.com; 

import android.content.Context; 
import android.graphics.Color; 
import android.text.Layout; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.squareup.picasso.Picasso; 

import java.util.ArrayList; 

/** 
* Created by RockyFish on 1/24/16. 
*/ 
public class LocalQueueAdapter extends BaseAdapter { 
     Context context; 
     LayoutInflater layoutInflater; 
     ArrayList<ArrayList<String>> localQueueInfo; 

     public LocalQueueAdapter(Context context, ArrayList<ArrayList<String>> localQueueInfo) 
     { 
      this.context = context; 
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      this.localQueueInfo = localQueueInfo; 
     } 
     @Override 
     public int getCount() { 
      return localQueueInfo.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return localQueueInfo.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     public class Holder 
     { 
      TextView songNameTextView, artistNameTextView, usernameTextView, numberEditText; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View rowView = layoutInflater.inflate(R.layout.local_queue_list_item, null); 
      Holder holder = new Holder(); 
      holder.songNameTextView = (TextView) rowView.findViewById(R.id.songNameTextView); 
      holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView); 
      holder.usernameTextView = (TextView) rowView.findViewById(R.id.usernameTextView); 
      holder.numberEditText = (TextView) rowView.findViewById(R.id.numberTextView); 


      holder.songNameTextView.setText(" " + localQueueInfo.get(position).get(1)); 
      holder.artistNameTextView.setText(" - " + localQueueInfo.get(position).get(2)); 
      holder.usernameTextView.setText(localQueueInfo.get(position).get(4)); 
      holder.numberEditText.setText(String.valueOf(position + 1) + "."); 

      if(position == 0) 
      { 
       rowView.setBackgroundColor(Color.parseColor("#45B8AC")); 
      } else if(position == 1) 
      { 
       rowView.setBackgroundColor(Color.parseColor("#74E2D5")); 
      } 
      return rowView; 
     } 
} 

謝謝!

+0

嘗試'機器人:layout_height =「50dp」'在''的RelativeLayout你 – Mohit

+0

的意思是你希望你的項目是50dp? –

+0

正確。 @Mohit我試過了,它沒有改變我的佈局 – Rockyfish

回答

0

問題是充氣器。我不確定爲什麼。下面的代碼應該適合你。此外,你不知道如何(這裏有一個例子和說明:https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html)使用ViewHolder

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     View convertView = LayoutInflater.from(context).inflate(R.layout.local_queue_list_item, parent, false); 
     Holder holder = new Holder(); 
     holder.songNameTextView = (TextView) rowView.findViewById(R.id.songNameTextView); 
     holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView); 
     holder.usernameTextView = (TextView) rowView.findViewById(R.id.usernameTextView); 
     holder.numberEditText = (TextView) rowView.findViewById(R.id.numberTextView); 
     convertView.setTag(holder); 
    } 

    Holder holder = convertView.getTag(); 

    .... 

    return convertView; 
}