【问题标题】:Floating Action Button overlaps checkbox in last cardview浮动操作按钮与最后一个卡片视图中的复选框重叠
【发布时间】:2017-06-09 16:24:03
【问题描述】:

我有一个包含 RecyclerView 和 FloatingActionButton 的布局文件。在里面我有多个 CardViews。 主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <include layout="@layout/toolbar"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/checkinput_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/checkinput_cv_top"
        android:layout_marginTop="8dp"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_arrow_forward_white_24dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/checkinput_btn_ready" />
</RelativeLayout>

卡片视图的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/cardview_checkinput_tv_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="8dp"
                android:text="Stadt:"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:layout_toStartOf="@+id/cardview_checkinput_cb" />

            <TextView
                android:id="@+id/cardview_checkinput_tv_input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/cardview_checkinput_tv_category"
                android:layout_alignStart="@+id/cardview_checkinput_tv_category"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginRight="8dp"
                android:text="TextView"
                android:layout_toStartOf="@+id/cardview_checkinput_cb" />

            <CheckBox
                android:id="@+id/cardview_checkinput_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

问题是卡片视图的复选框位于卡片视图的右侧。 它看起来像这样:

如您所见,最后一个复选框不可见,因为 FloatingActionButton 与它重叠。问题是recyclerview中的cardviews数量每次都不一样。 cradviews 是以编程方式添加的,所以我不能只设置最后一个的 CheckBox 应该在 xml 文件中具有 toStartOf -> FloatingActionButton。

所以我在适配器中以编程方式尝试了它:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.cb.getLayoutParams();
params.addRule(RelativeLayout.START_OF, R.id.checkinput_btn_ready);
holder.cb.setLayoutParams(params);

这对我不起作用,但我不知道如何将最后一个卡片视图的复选框移动到 FloatingActionButton 的左侧。

【问题讨论】:

    标签: android android-recyclerview floating-action-button


    【解决方案1】:

    将您的 recyclerview 代码更改为此

       <android.support.v7.widget.RecyclerView
            android:id="@+id/checkinput_rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="100dp"
            android:clipToPadding="false"
            android:layout_below="@+id/checkinput_cv_top"
            android:layout_marginTop="8dp"/>
    

    如果它不尝试增加 paddingbottom,这将在 fab 图标上方显示您的最后一个项目。

    【讨论】:

    • 它会起作用,但它并不是我正在寻找的。​​span>
    【解决方案2】:

    您是否尝试过获取适配器计数(然后获取最后一项)并在您的复选框中设置 margin-right?

    【讨论】:

    • 发布你的结果 =)
    • 我刚刚将它添加到我的适配器 onBindViewHolder 方法中: if(position == items.size()-1) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.cb.getLayoutParams(); params.setMargins(params.leftMargin, params.topMargin, params.rightMargin+120, params.bottomMargin); holder.cb.setLayoutParams(params); }
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 2020-03-19
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2021-08-21
    相关资源
    最近更新 更多