如何在父布局的右上角添加两个浮动操作按钮(一半在布局内,一半在外面),并在点击时显示

问题描述 投票:1回答:2

我想当用户点击自定义tableview的某一行时,在parrent布局的右上角位置显示两个浮动按钮,但是在布局中显示一半,在布局外显示一半。

这是一个例子:

考虑到底部布局在我的表格视图行中,

这是我要添加fab的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainAddEditIcons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/tvLAyout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:paddingLeft="20dp"
            android:textColor="@color/black" />
    </LinearLayout>

</LinearLayout>
android floating-action-button
2个回答
0
投票

你可以试试这段代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#E0E0E0">

    <RelativeLayout
        android:id="@+id/top_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:alpha="1.0"
            android:scaleType="centerCrop"
            tools:src="@drawable/food_1" />

        <View
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/gradient_up" />

        <!-- Back button -->

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_show_rating_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/top_card"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="-28dp"
        android:layout_marginRight="16dp"
        app:srcCompat="@drawable/ic_add_white_24px" />

    <!-- Ratings -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_card"
        android:background="@android:color/transparent"
        android:clipToPadding="false"
        android:paddingBottom="16dp"
        android:paddingTop="28dp"
        android:visibility="gone"
        tools:listitem="@layout/item_rating" />

</RelativeLayout>

0
投票

试试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainAddEditIcons"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/tvLAyout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:background="@color/colorblueLight"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:padding="10dp"
                android:text="NIlu" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/tvLAyout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/colorAccent"
            android:orientation="vertical" />


    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/l_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:clickable="true"
        android:src="@drawable/floating_add"
        app:backgroundTint="@color/colorOrange"
        app:fabSize="normal"
        app:layout_anchor="@id/tvLAyout1"
        app:layout_anchorGravity="bottom|right" />

</android.support.design.widget.CoordinatorLayout>

OUTPUT

enter image description here

试试这个

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


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

        <LinearLayout
            android:id="@+id/nilu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:orientation="vertical"
            android:paddingBottom="30dp">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/nilu2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/nilu"
            android:background="@color/colorGray"
            android:orientation="vertical"
            android:paddingTop="30dp">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="NILU" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/nilu"
            android:layout_marginTop="-15dp"
            android:orientation="horizontal">

            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:src="@mipmap/ic_launcher_round"
                app:elevation="2dp" />


        </RelativeLayout>


    </RelativeLayout>


</RelativeLayout>

OUTPUT

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.