Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android列表片段重叠_Java_Android_Listview_Android Listfragment - Fatal编程技术网

Java Android列表片段重叠

Java Android列表片段重叠,java,android,listview,android-listfragment,Java,Android,Listview,Android Listfragment,我正在学习android开发,我被困在片段中。我已经阅读了教程要点书和一些android开发者文档,所以下一步是为我的手机构建一个应用程序 我想列一张购物单。启动应用程序时,会显示一个菜单,其中显示三个选项,有问题的选项是第三个 其想法是允许用户创建更新并从列表中删除产品 这就是我到目前为止所做的 product_crud.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:/

我正在学习android开发,我被困在
片段中。我已经阅读了教程要点书和一些android开发者文档,所以下一步是为我的手机构建一个应用程序

我想列一张购物单。启动应用程序时,会显示一个菜单,其中显示三个选项,有问题的选项是第三个

其想法是允许用户创建更新并从列表中删除产品

这就是我到目前为止所做的

product_crud.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="116dp">

    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/name_txt"
    android:hint="@string/product_name"
    android:inputType="text"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_add"
        android:paddingLeft="10px"
        android:text="@string/add_product"
        android:layout_below="@+id/name_txt"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/android:list"
    android:layout_below="@+id/btn_add"
    android:layout_alignParentStart="true" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10px"
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_delete"
    android:text="@string/remove_product"
    android:layout_gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_name"
    android:textSize="40px"
    android:layout_alignTop="@+id/btn_delete"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/btn_delete" />


 </RelativeLayout>
<?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="vertical" >

    <FrameLayout
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>
适配器

public class CrudArrayAdapter extends ArrayAdapter<Product> {

private final List<Product> list;
private final Activity context;

static class ViewHolder{
    protected TextView name;
    protected Button delete;
}


public CrudArrayAdapter(Activity context,List<Product> list) {
    super(context, R.layout.rowlayout,list);
    this.list = list;
    this.context = context;
}


@Override
public View getView(int position,View convertView,ViewGroup parent){
    View view;
    if(convertView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.crudrowlayout,null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.name = (TextView)view.findViewById(R.id.text_name);
        viewHolder.delete = (Button)view.findViewById(R.id.btn_delete);
        view.setTag(viewHolder);
    }else{
        view = convertView;
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.name.setText(list.get(position).getName());
    return view;
}


}
不要担心数据源或适配器,这里的问题是
列表视图
编辑文本
按钮
(deleteBtn)重叠。 列表视图显示在
编辑文本
按钮
上方。我试着用不同的方式改变布局,但没有一个奏效

我已经读到,您可以通过一个普通的活动和一个
ListView
来实现同样的功能,但是我想学习如何用这种方式来实现

一些想法

问题的图片:


您的主视图是LinearLayout,请尝试将其更改为RelativeLayout。 您应该这样尝试:

<?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"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_alignParentTop="true"
        android:id="@+id/buttonView"
        android:layout_width="match_parent"
        android:layout_height="116dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name_txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:inputType="text" />

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/name_txt"
            android:paddingLeft="10px"
          />

    </RelativeLayout>

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/buttonView" />

</RelativeLayout>

这将导致:


我注意到一些事情可能会对你有所帮助

首先,
CrudProducts扩展了ListActivity
PM\u List\u Fragment扩展了ListFragment
。如果您打算显示一个列表,您应该只依赖于
ListFragment
。您可以使
CrudProducts
简单地扩展
活动

如果这样做,您可以使用
setContentView(R.layout.crud\u活动)

crud_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="116dp">

    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/name_txt"
    android:hint="@string/product_name"
    android:inputType="text"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_add"
        android:paddingLeft="10px"
        android:text="@string/add_product"
        android:layout_below="@+id/name_txt"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/android:list"
    android:layout_below="@+id/btn_add"
    android:layout_alignParentStart="true" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10px"
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_delete"
    android:text="@string/remove_product"
    android:layout_gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_name"
    android:textSize="40px"
    android:layout_alignTop="@+id/btn_delete"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/btn_delete" />


 </RelativeLayout>
<?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="vertical" >

    <FrameLayout
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>
第三,product_crud.xml也需要一些改进

您的
不需要以下属性,因为
列表视图的父级
不是一个相对值

android:layout_below="@+id/btn_add"
android:layout_alignParentStart="true"
此外,您的孩子不需要
RelativeLayout

android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
相反,试着让你的
按钮
与父母匹配

<Button
    android:id="@+id/btn_add"
    android:layout_width="match_parent"

以上内容将解决您的问题,列表应正确可见。

请仅发布相关代码/布局。读者浏览多个文件时会感到非常困惑,只需找到相关的文件即可。您好,您的问题出在片段管理器中。尝试用
replace()
方法替换
add()
片段。比如:
transaction.replace(R.id.fragment\u容器,newFragment);transaction.addToBackStack(null);//提交事务。提交()[谢谢,我已经试过了,在设计视图中它看起来与此完全一样,但是它不起作用。我在问题中添加了问题的图片。
android:paddingLeft="10dp"
android:paddingRight="10dp"