我是android应用程序开发的新手。我遇到了关于在列表视图末尾显示按钮的问题。我正在使用线性布局。应用程序可以显示所有列表但不能显示按钮。我还在这里粘贴了我的 XML 代码。在这方面的任何帮助,将不胜感激。
莫汉
主要的.xml
<?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="fill_parent"
>
<ImageView
android:id="@+id/contact_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_New"
android:width="170dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:text="Click"
/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btn_New"
android:text="@string/hello"
/>
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/textView"
/>
</RelativeLayout>
联系人列表项.xml
<?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" android:orientation="horizontal" android:weightSum="1">
<TextView
android:id="@+id/txtDisplayName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<ImageView android:id="@+id/contact_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
</LinearLayout>
ContactListActivity.java
package com.contactlist;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Data;
public class ContactListActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContactList contactList=this.getContacts();
ArrayAdapter<Contact> adapter=new ContactAdapter(this,contactList.getContacts());
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Object o=this.getListAdapter().getItem(position);
Contact c=(Contact)o;
Toast.makeText(this, c.getDisplayName(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, c.getId(), Toast.LENGTH_SHORT).show();
}
private ContactList getContacts()
{
ContactList contactList=new ContactList();
Uri uri=ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr=getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur=cr.query(uri, null, null, null, sortOrder);
if(cur.getCount() >0)
{
String id;
String img;
String name;
while(cur.moveToNext())
{
Contact c =new Contact();
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
img= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
final Bitmap photo;
if(img != null) {
photo = queryContactBitmap(img);
} else {
photo = null;
}
c.setId(id);
c.setImage(photo);
c.setDisplayName(name);
contactList.addContact(c);
}
}
// cur.close();
return contactList;
}
private Bitmap queryContactBitmap(String photoId) {
final Cursor photo = managedQuery(
Data.CONTENT_URI,
new String[] {Photo.PHOTO}, // column where the blob is stored
Data._ID + "=?", // select row by id
new String[]{photoId}, // filter by the given photoId
null);
final Bitmap photoBitmap;
if(photo.moveToFirst()) {
byte[] photoBlob = photo.getBlob(
photo.getColumnIndex(Photo.PHOTO));
photoBitmap = BitmapFactory.decodeByteArray(
photoBlob, 0, photoBlob.length);
} else {
photoBitmap = null;
}
photo.close();
return photoBitmap;
}
}
联系人列表.java
package com.contactlist;
import java.util.ArrayList;
import java.util.List;
public class ContactList {
private List<Contact> _contacts=new ArrayList<Contact>();
public List<Contact> getContacts(){return _contacts;}
public void addContact(Contact contact){ this._contacts.add(contact);}
}
ContactAdapter.java
package com.contactlist;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ContactAdapter extends ArrayAdapter<Contact> {
private final List<Contact> _contacts;
private final Activity _context;
public ContactAdapter(Activity context, List<Contact> contacts)
{
super(context,R.layout.main,contacts);
this._contacts=contacts;
this._context=context;
}
static class ViewHolder {
protected TextView text;
private Contact _contact;
public ImageView imageview;
protected void setContact(Contact contact)
{
text.setText(contact.getDisplayName());
imageview.setImageBitmap(contact.getImage());
_contact=contact;
}
protected Contact getContact() {return _contact;}
}
@Override
public Contact getItem(int position)
{
return _contacts.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view=null;
if(convertView==null)
{
LayoutInflater inflater=_context.getLayoutInflater();
view=inflater.inflate(R.layout.contactlistitem, null);
final ViewHolder viewHolder=new ViewHolder();
viewHolder.text=(TextView)view.findViewById(R.id.txtDisplayName);
viewHolder.imageview =(ImageView)view.findViewById(R.id.contact_image);
viewHolder.setContact(_contacts.get(position));
view.setTag(viewHolder);
}
else
{
view = convertView;
}
return view;
}
}
联系方式.java
package com.contactlist;
import java.util.ArrayList;
import android.R.string;
import android.graphics.Bitmap;
public class Contact {
private String _id;
private String _displayName;
private Bitmap _img;
public String getId(){return _id;}
public String getDisplayName(){return _displayName;}
public Bitmap getImage(){return _img;}
public void setId(String id){_id=id;}
public void setDisplayName(String displayName){_displayName=displayName;}
public void setImage(Bitmap img){_img=img;}
}