0

我是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;}
}
4

3 回答 3

1

试试这个,看看它是否有效:

<?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>

MainActivity 类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.main);

    ListView listView = (ListView) findViewById(R.id.contactList);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
              "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
              "Linux", "OS/2" };

    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);
  }

在此处输入图像描述

现在当你把东西放在里面时,listView它不应该“按下”按钮或textView向下。

于 2012-08-11T05:08:17.520 回答
0

首先,您必须在主要相对布局中设置线性布局并设置其重力底部

<RelativeLayout
android:width = "fill..."
android....>

<LinearLayout
android:gravity="bottom"
android:id="@+id/linear1"  
android:layout_alignParentBottom="true">
<Button/>

</LinearLayout>


<ListView
android:layout_above="@id/linear"/>
于 2012-08-10T05:14:51.113 回答
0

安卓:layout_weight="1"

删除它。

如果您没有在包含某些组件的级别上表达 'android:layout_weightSum' 表达 'android:layout_weight' 但包含 'android:layout_weight="1"' 代码的组件正好将大小更改为全屏。

(对不起,我英语说得不太好......)

  1. android:layout_weight="1" 是错误的
  2. android:layout_weight="1" 删除它
  3. 或者先使用 android:layout_weightSum 。
于 2012-08-10T05:15:42.910 回答