线性布局中的浮动操作按钮未显示

floating action button in linearlayout not showing

您好,我正在尝试将浮动操作按钮对齐到屏幕的右下角,但它显示在屏幕的右上角,我该怎么做才能使其位于右下角?

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:theme="@style/AppTheme"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/createRoomButton"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_add_black_24dp"/>

    <SearchView
        android:id="@+id/searchInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:queryHint="Search Rooms">
        <requestFocus />
    </SearchView>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/pullToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/roomList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>






</LinearLayout>

已为您的 UI 进行必要的更改,现在它可以完美运行:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:theme="@style/AppTheme"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/createRoomButton"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="22dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:srcCompat="@drawable/ic_add_black_24dp"/>



    <SearchView
        android:id="@+id/searchInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:layout_marginTop="50dp"
        android:queryHint="Search Rooms">
        <requestFocus />
    </SearchView>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/pullToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/roomList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </RelativeLayout>
    </LinearLayout>