Android 动态获取(设置)listView行高

Android 动态获取(设置)listView行高,android,listview,android-arrayadapter,row-height,Android,Listview,Android Arrayadapter,Row Height,我有listView,它的每一行都是这样的: <?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="wrap_content" android:orientation=

我有listView,它的每一行都是这样的:

<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" 
    android:background="#FFFFFF">
    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <TextView
            android:id="@+id/altOrderId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ID"
            android:gravity="center|center"
            android:textColor="#040404"
            android:textSize="15dp"
            android:textStyle="bold"
            android:typeface="sans" />
    </LinearLayout>

    <TextView
        android:id="@+id/altOrderTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dip"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Just gona stand there and ..."
        android:textColor="#000000"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/altOrderStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/altOrderTitle"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="5dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#838B8B"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/altOrderPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/thumbnail"
        android:text="Rihanna Love the way lie"
        android:textColor="#458B74"
        android:textSize="15dp"
        android:textStyle="bold"
        android:typeface="sans" />

</RelativeLayout>

我的适配器类是:

public class OrderAdapter extends ArrayAdapter<Order>{


    Context context; 
    int layoutResourceId;    
    public static int rowHeight = 0;
    List<Order> orders = null;
    private Activity activity;
    public OrderAdapter(Context context,  int layoutResourceId,List<Order> orders) {
        super(context, layoutResourceId, orders);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.orders = orders;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        OrderHolder holder = null;

        if(row == null)
        {


            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.dash_alt_item, parent, false);
            row.setBackgroundColor(Color.WHITE);
            //row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,30));
            holder = new OrderHolder();
            holder.orderId = (TextView)row.findViewById(R.id.altOrderId);
            holder.orderTitle = (TextView)row.findViewById(R.id.altOrderTitle);
            holder.orderStatus = (TextView)row.findViewById(R.id.altOrderStatus);
            holder.orderPrice = (TextView)row.findViewById(R.id.altOrderPrice);

            row.setTag(holder);
        }
        else
        {
            holder = (OrderHolder)row.getTag();
        }

        Order order = orders.get(position);
        holder.orderId.setText(Integer.toString(order.getOrderid()));
        holder.orderTitle.setText(order.getTitle());
        holder.orderStatus.setText(order.getProcess_status().getProccessStatusTitle());
        holder.orderPrice.setText(Float.toString(order.getPrice()));

        return row;
    }

    static class OrderHolder
    {
        TextView orderId;
        TextView orderTitle;
        TextView orderStatus;
        TextView orderPrice;
    }
}
公共类OrderAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
公共静态整数行高=0;
列表顺序=空;
私人活动;
public OrderAdapter(上下文上下文、内部布局资源ID、列表订单){
超级(上下文、布局资源ID、订单);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个。订单=订单;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
OrderHolder=null;
if(行==null)
{
LayoutFlater充气器=(LayoutFlater)this.context.getSystemService(context.LAYOUT\u充气器\u服务);
行=充气机。充气(R.layout.dash\u alt\u项目,父项,false);
行.背景色(颜色.白色);
//row.setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,30));
holder=新订单持有者();
holder.orderId=(TextView)row.findViewById(R.id.altOrderId);
holder.orderTitle=(TextView)row.findViewById(R.id.altOrderTitle);
holder.orderStatus=(TextView)row.findViewById(R.id.altOrderStatus);
holder.orderPrice=(TextView)row.findViewById(R.id.altOrderPrice);
row.setTag(支架);
}
其他的
{
holder=(OrderHolder)行。getTag();
}
订单=订单。获取(位置);
holder.orderId.setText(Integer.toString(order.getOrderid());
holder.orderTitle.setText(order.getTitle());
holder.orderStatus.setText(order.getProcess_status().getProcessStatusTitle());
holder.orderPrice.setText(Float.toString(order.getPrice());
返回行;
}
静态类订单持有者
{
文本视图医嘱ID;
文本视图订单标题;
text查看订单状态;
text查看订单价格;
}
}

我需要检测(或设置)每个listview行的高度,并根据设备屏幕高度显示相应数量的项目。如何实现它?

使用gridview而不是listview,并将其设置为android:numColumns=“auto\u fit”>


否。我需要检测设备的高度并下载相应的项目数量。所以数量将与设备高度相对应。我编辑了我的答案,您可以使用getWindowManager().getDefaultDisplay();)我知道如何检测设备高度。我不知道如何检测listview的项目高度,也不知道如何设置它。因此,您不需要用绝对值设置高度listview,只需给它的高度=填充父项即可。
<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit" >
    </GridView>
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;