【问题标题】:How to delete the space between parent and the cell?如何删除父级和单元格之间的空间?
【发布时间】:2019-06-20 08:26:53
【问题描述】:

我在父母和我的单元格之间有一个边界。怎么删除?

我搜索但我没有找到我的代码中的问题。

主类:

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 内容主体:

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

【问题讨论】:

  • LinearLayoutcell layout file 中删除android:layout_margin="15dp"。您可能还想删除 android:background="@drawable/list_cell_border_main" 并将其替换为仅背景颜色,因为可以使用 DividerItemDecoration 自定义每行/单元格之间的分隔线
  • 您的单元格 xml 中有 android:layout_margin="15dp"。删除它应该可以工作。
  • 谢谢。这是我的问题的解决方案

标签: java android xml android-layout layout


【解决方案1】:
android:layout_margin="15dp"

这是您的问题,您正在添加边距,而您不希望它出现在单元格的主 LinearLayout 中。

删除那条线,你应该会很好。


旁注:

由于您使用RecyclerView 而不是使用android:background="@drawable/list_cell_border_main" 为单元格添加边框,因此请使用DividerItemDecoration 查看这个很棒的SO Answer,它会指导您如何做到这一点。

【讨论】:

  • @Vich 很高兴它成功了 :) 您应该将问题标记为正确,这样任何发现此问题的人都知道正确答案。
  • 是的,我这样做了;)
【解决方案2】:

问题出在android:layout_margin="15dp" 中的LinearLayout ;)

【讨论】:

    猜你喜欢
    • 2020-03-25
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多