Android 以线性布局显示ListView末尾的按钮
我是android应用程序开发的新手。我遇到了一个关于在列表视图末尾显示按钮的问题。我使用线性布局。应用程序可以显示所有列表,但不能显示按钮。我还在这里粘贴了我的XML代码。我们将非常感谢您在这方面提供的任何帮助 默翰 main.xmlAndroid 以线性布局显示ListView末尾的按钮,android,button,android-linearlayout,Android,Button,Android Linearlayout,我是android应用程序开发的新手。我遇到了一个关于在列表视图末尾显示按钮的问题。我使用线性布局。应用程序可以显示所有列表,但不能显示按钮。我还在这里粘贴了我的XML代码。我们将非常感谢您在这方面提供的任何帮助 默翰 main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<?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>
首先,您必须在主相对布局中设置线性布局,并设置其重力底部
<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"/>
android:layout\u weight=“1”
删除它
如果您不是在包含部分组件的超级别上表达“android:layout\u weightSum”,则某些组件是表达“android:layout\u weight”,但也包含“android:layout\u weight=“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>
主要活动类别:
@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);
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView ListView=(ListView)findViewById(R.id.contactList);
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”};
//第一个参数-上下文
//第二个参数-行的布局
//第三个参数-写入数据的TextView的ID
//第四,数据数组
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,android.R.id.text1,值);
//将适配器分配给ListView
setAdapter(适配器);
}
现在,当您将内容放入
列表视图
时,它不应“按下”按钮或文本视图
向下 你对main.xml上的图像视图做了什么?我试图从android手机联系人列表中提取图像,这就是为什么我使用图像视图标记,但即使在删除它并重新运行代码后,输出仍然是相同的:-)我测试了你的main.xml,我确实在图形布局中看到了一个按钮。当你向listview添加东西时,你是否看不到按钮?是的,你是对的。当列表视图加载联系人姓名和图像时,按钮不会出现在末尾。你知道如何解决这个问题吗?看看我的答案,可能会有帮助。非常感谢你的回复,但即使删除了android:layout_weight=“1”,底部也看不到该按钮。哦,很抱歉。我更新了我的代码,所以现在它不应该给你那个错误。我不知道这是否可以解决按钮问题,请尝试一下。当我尝试使用您提供的XML代码时,XML解析器在“”和“”处抱怨。因此,我删除了它并尝试运行,然后我的布局仍然没有改变,尽管我在图形布局中看到了按钮。我已在此[链接]中附上布局图像。任何关于这个问题的想法:-)不,不要删除它。我更新了我的代码…按原样粘贴代码,看看是否有效。我尝试使用更新的版本,但结果仍然相同。我已更新了答案,将onCreate中的代码放入主活动中。我已经用我更新的main.xml测试了该代码,效果很好。按钮没有按下。
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;
}
}
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;}
}
<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"/>
<?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>
@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);
}