简体   繁体   English

如何将浮动操作按钮对齐到结尾|底部

[英]How to align Floating Action Button to end|bottom

I have one FloatingActionButton .我有一个FloatingActionButton I want it to be end|bottom like我希望它end|bottom

What I want我想要的是

this.这个。

Currently, My design is like this目前,我的设计是这样的

What I have我拥有的

I want it to be at the exact end|bottom of the screen.我希望它位于屏幕的确切末端|底部。 How to do that?怎么做? Which layout is best to use?哪种布局最适合使用?

This is my whole xml code这是我的整个 xml 代码

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:padding="30dp">


    <ImageView
        android:id="@+id/post_request_back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:src="@drawable/general_back_icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/muli_black"
        android:text="@string/_post_request_header_name"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:textSize="30sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/how_requests_work"
        android:textColor="@color/colorAccent"
        android:textSize="10sp" />

    <ListView
        android:id="@+id/postlist"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="0dp"
        app:layout_constraintBottom_toTopOf="@+id/postadd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/postadd"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@drawable/postrequest_add_btn" />


</LinearLayout> 

You can wrap your FAB button into a RelativeLayout and keep other views in your LinearLayout您可以将 FAB 按钮包装到一个RelativeLayout并在LinearLayout保留其他视图

<?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"
    android:padding="30dp">

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

        <ImageView
            android:id="@+id/post_request_back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:src="@drawable/general_back_icon" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:fontFamily="@font/muli_black"
            android:text="@string/_post_request_header_name"
            android:textAllCaps="true"
            android:textColor="@color/colorAccent"
            android:textSize="30sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/how_requests_work"
            android:textColor="@color/colorAccent"
            android:textSize="10sp" />

        <ListView
            android:id="@+id/postlist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" />

    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/postadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/postrequest_add_btn" />


</RelativeLayout>

Note: I removed the Constraints you added to the ListView as they are related to ConstraintLayout , also made its height as wrap_content注意:我删除了您添加到ListView ConstraintLayout ,因为它们与ConstraintLayout相关,并将其高度设为wrap_content

And here is with the ConstraintLayout这是ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="30dp">

    <ImageView
        android:id="@+id/post_request_back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:src="@drawable/general_back_icon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_post_request_header_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/muli_black"
        android:text="@string/_post_request_header_name"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_request_back_button" />

    <TextView
        android:id="@+id/id_how_requests_work"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/how_requests_work"
        android:textColor="@color/colorAccent"
        android:textSize="10sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_post_request_header_name" />

    <ListView
        android:id="@+id/postlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_how_requests_work" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/postadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="16dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/postrequest_add_btn" />

</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM