如何删除父级与单元格之间的空间?

时间:2019-06-20 08:26:53

标签: java android xml android-layout layout

我的父母与我的牢房之间有一个边界。如何删除它?

我进行了搜索,但在代码中找不到问题所在。

主类:

final RecyclerView rv = (RecyclerView) findViewById(R.id.contentMain_rv_1);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new MainActivityRecyclerViewAdapter());

适配器:

public class MainActivityRecyclerViewAdapter extends RecyclerView.Adapter<MainActivityRecyclerViewAdapter.MyViewHolder> {

    private final List<Pair<String, String>> characters = Arrays.asList(
            Pair.create("Lyra Belacqua", "01/01/2001"),
            Pair.create("Pantalaimon", "01/01/2002."),
            Pair.create("Roger Parslow", "01/01/2003"),
            Pair.create("Lord Asriel", "01/01/2004"),
            Pair.create("Marisa Coulter", "01/01/2005."),
            Pair.create("Iorek Byrnison", "01/01/2006."),
            Pair.create("Serafina Pekkala", "01/01/2007."),
            Pair.create("Lee Scoresby", "01/01/2008."),
            Pair.create("Ma Costa", "01/01/2009"),
            Pair.create("John Faa", "01/01/2010")
    );

    @Override
    public int getItemCount() {
        return characters.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_cell_main, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Pair<String, String> pair = characters.get(position);
        holder.display(pair);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final TextView description;

        private Pair<String, String> currentPair;

        public MyViewHolder(final View itemView) {
            super(itemView);

            name = ((TextView) itemView.findViewById(R.id.name));
            description = ((TextView) itemView.findViewById(R.id.description));

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(itemView.getContext())
                            .setTitle(currentPair.first)
                            .setMessage(currentPair.second)
                            .show();
                }
            });
        }

        public void display(Pair<String, String> pair) {
            currentPair = pair;
            name.setText(pair.first);
            description.setText(pair.second);
        }
    }

XML单元格:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:background="@drawable/list_cell_border_main"
    android:orientation="vertical">


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/ic_dialog_info" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:ellipsize="end"
            android:lines="1"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="22sp"
            android:textStyle="bold"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Description"></TextView>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/description"
            tools:text="Personnage"></TextView>

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/ic_menu_info_details" />
    </android.support.constraint.ConstraintLayout>

XML content_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/contentMain_rv_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>


这是我的应用程序的屏幕,带有要删除的红色边框:

https://i.imgur.com/yqPp86o.png

这是我想要的结果:

https://i.imgur.com/0revzIK.png

2 个答案:

答案 0 :(得分:2)

android:layout_margin="15dp"

有一个问题,您要添加边距,而您不希望在单元格的主LinearLayout中添加边距。

删除该行,您应该会很好。


旁注:

由于您使用的是RecyclerView而不是android:background="@drawable/list_cell_border_main"来向单元格添加边框,因此请使用DividerItemDecoration来看看这个很棒的SO Answer,它会指导您如何精确地执行操作那个。

答案 1 :(得分:0)

问题出在android:layout_margin="15dp"的{​​{1}}上;)