在动态列表视图项上设置高度

时间:2015-08-25 13:30:55

标签: android android-layout android-activity

我是Android开发的新手,我在设计方面遇到了麻烦。我正在尝试制作短信应用。我成功访问了收件箱并显示了联系人姓名。现在我想在每个listview-item上设置一个自定义高度,并删除看起来像底部边框的分隔线高度。 android:minHeight在我的案例中似乎不起作用。我还想在每个动态创建的listview-item上添加click事件,但它似乎不起作用。

这是我的代码:

MainActivity.java

public class MainActivity extends ListActivity {

    ArrayList<String> messages = new ArrayList<String>();

    ArrayAdapter<String> adapter;
    ArrayList<String> msglist = new ArrayList<String>();
    String num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lvmsgs = (ListView) findViewById(R.id.msglist);
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
        String sms = null;
        ContentResolver resolver = null;
        while (cur.moveToNext()) {
            sms += "From :" + cur.getString(2) + " : " + cur.getString(cur.getColumnIndexOrThrow("body")) + "\n";
            num = cur.getString(2);
            num = num.replace("+639", "09");
            if (msglist.contains(num)) {

            } else {
                msglist.add(num);
                messages.add(getContactName(num).toString());
            }
        }
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messages);
        setListAdapter(adapter);
        lvmsgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "Toast",
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    private String getContactName(String number) {

        String name = null;
        String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
        Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);

        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        } else {
            return number;
        }
        return name;
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/msglist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:descendantFocusability="blocksDescendants"
        android:focusable="false"
        android:focusableInTouchMode="false" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

我认为您可以将自己的布局传递给arrayadapter。只创建一个没有任何布局的文本视图的布局。并将高度和填充设置为文本视图。

答案 1 :(得分:0)

Self

}

MainActivity:     package com.example.evuser.customlistviewdemo;

activity_main:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

<ListView
    android:id="@+id/lvCustomList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

layout_list_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="10dp">
    <ImageView
    android:id="@+id/ivIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/start1" />
   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Title" />
    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Description" />
</LinearLayout>
</LinearLayout>

ModelClass:

package com.example.evuser.customlistviewdemo;

public class ListData {

String Description;
String title;
int imgResId;

public String getDescription() {
    return Description;
}

public void setDescription(String description) {
    Description = description;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getImgResId() {
    return imgResId;
}

public void setImgResId(int imgResId) {
    this.imgResId = imgResId;
}

}

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {

ListView lvDetail;
Context context = MainActivity.this;
ArrayList<ListData> myList = new ArrayList<ListData>();

String[] title = new String[]{
        "Title 1", "Title 2", "Title 3", "Title 4",
        "Title 5", "Title 6", "Title 7", "Title 8"
};
String[] desc = new String[]{
        "Desc 1", "Desc 2", "Desc 3", "Desc 4",
        "Desc 5", "Desc 6", "Desc 7", "Desc 8"
};
int[] img = new int[]{
       R.drawable.start1, R.drawable.star2, R.drawable.star3, R.drawable.star4,
        R.drawable.star5, R.drawable.star4, R.drawable.star7, R.drawable.star8
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lvDetail = (ListView) findViewById(R.id.lvCustomList);
    // insert data into the list before setting the adapter
    // otherwise it will generate NullPointerException  - Obviously
    getDataInList();
    lvDetail.setAdapter(new MyBaseAdapter(context, myList));
}

private void getDataInList() {
    for (int i = 0; i < title.length; i++) {
        // Create a new object for each list item
        ListData ld = new ListData();
        ld.setTitle(title[i]);
        ld.setDescription(desc[i]);
        ld.setImgResId(img[i]);
        // Add this object into the ArrayList myList
        myList.add(ld);
    }
}

import java.util.ArrayList;

AdapterClass:

package com.example.evuser.customlistviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

}

public class MyBaseAdapter extends BaseAdapter {

ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;


public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public ListData getItem(int position) {
    return myList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder mViewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
        mViewHolder = new MyViewHolder(convertView);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    ListData currentListData = getItem(position);

    mViewHolder.tvTitle.setText(currentListData.getTitle());
    mViewHolder.tvDesc.setText(currentListData.getDescription());
    mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());

    return convertView;
}

private class MyViewHolder {
    TextView tvTitle, tvDesc;
    ImageView ivIcon;

    public MyViewHolder(View item) {
        tvTitle = (TextView) item.findViewById(R.id.tvTitle);
        tvDesc = (TextView) item.findViewById(R.id.tvDesc);
        ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
    }
}