簡體   English   中英

在屏幕底部對齊布局

[英]Align layout at the bottom of the screen

我希望將其與包含掃描信息小部件(進度條,文本視圖和按鈕)的附件相同,以保留在屏幕底部。 但是,當我運行我的應用程序時,當ListView為空時,它們將位於屏幕頂部。

在此處輸入圖片說明

我嘗試了其他主題中介紹的許多方法,但仍然無法使它起作用。

對於這種情況下應該使用哪種布局,您有什么建議嗎?

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

    <ListView
        android:id="@+id/ListViewDevices"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ListViewDevices">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBarDevice"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/lblScanningStatus"
                app:layout_constraintStart_toStartOf="parent" />

            <TextView
                android:id="@+id/lblScanningStatus"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="20dp"
                android:text="@string/scan_in_progress"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toEndOf="@+id/progressBarDevice" />

            <Button
                android:id="@+id/btnPauseScan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="@string/pause_scan"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

這是我以前使用的應用程序的實現方式。 使用RelativeLayout作為主外殼,我們可以在其中以任何方式定位元素。

<?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:layout_weight="10"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:background="@color/grey_font">

        <ProgressBar
            android:id="@+id/progressBarDevice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp" />

        <TextView
            android:id="@+id/lblScanningStatus"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="24dp"
            android:layout_toRightOf="@id/progressBarDevice"
            android:text="Scanning" />

        <Button
            android:layout_marginRight="16dp"
            android:layout_centerVertical="true"
            android:background="@color/orange_pay"
            android:id="@+id/btnPauseScan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"
            android:text="PAUSE" />

    </RelativeLayout>

</RelativeLayout>

布局快照

首先,我將其稍微扁平化以簡化布局。 刪除了保存底部約束的FrameLayout。然后使listViewDevices將底部約束為底部約束布局的頂部(我稱之為掃描儀)。

在我的布局編輯器中看起來正確。

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

<ListView
    android:id="@+id/ListViewDevices"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="60dp"
    app:layout_constraintBottom_toTopOf="@id/scanner"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/scanner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintHorizontal_bias="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/ListViewDevices"
    app:layout_constraintBottom_toBottomOf="parent">

    <ProgressBar
        android:id="@+id/progressBarDevice"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/lblScanningStatus"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/lblScanningStatus"
        android:layout_width="wrap_content"
        android:layout_height="24dp"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="20dp"
        android:text="SCANNING FOR NEW DEVICES..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/progressBarDevice" />

    <Button
        android:id="@+id/btnPauseScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="PAUSE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

我寧願將LinearLayout添加為根布局。 因此,當您向下或向上滾動時,它為空時將不會隨着您的布局滾動。

因此,當您的列表視圖為空時,它也將僅在屏幕底部。

 <?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:orientation="vertical" > <ListView android:id="@+id/ListViewDevices" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="60dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <FrameLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ListViewDevices"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar android:id="@+id/progressBarDevice" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/lblScanningStatus" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/lblScanningStatus" android:layout_width="wrap_content" android:layout_height="24dp" android:layout_marginStart="8dp" android:layout_marginBottom="20dp" android:text="@string/scan_in_progress" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/progressBarDevice" /> <Button android:id="@+id/btnPauseScan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="@string/pause_scan" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </LinearLayout> </FrameLayout> </LinearLayout> 

感謝您的回答,但沒有一個有效。 有時在Android Studio中看起來不錯,但是當我在真實電話上測試我的應用程序時,小部件會顯示在屏幕頂部。 如果我找到可行的解決方案,請在此處發布。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM