【问题标题】:How to Make An Android Progressbar as Overlay Over a Layout如何使 Android 进度条覆盖在布局上
【发布时间】:2020-03-09 11:40:37
【问题描述】:

我有以下布局文件。我需要在布局中显示一个进度条。目前进度条在中间占用空白空间,所以它在显示栏时将我的顶部和底部视图分开,但我试图将进度条显示为布局上的覆盖,所以它不会占用任何空间。我怎样才能做到这一点

这是我目前拥有的布局文件

<?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="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imgLeftArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/left_arrow2"></ImageView>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal">

            <ImageView
                android:id="@+id/imgGreendot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/green_dot"></ImageView>

            <TextView
                android:id="@+id/tvStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ></TextView>

        </LinearLayout>


        <ImageView
            android:id="@+id/imgBid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           ></ImageView>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvExpected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/tvTotalBid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total Bids | "
            android:textColor="#a9a9a9"
            android:textSize="14dp"></TextView>

        <TextView
            android:id="@+id/tvDeclined"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
    </LinearLayout>


    <ProgressBar
        android:id="@+id/progressBar5"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="visible" />


    <LinearLayout
        android:id="@+id/layoutswitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_weight="1"
            android:text="Show Active Drivers Only" />
    </LinearLayout>


</LinearLayout>

【问题讨论】:

  • 你需要使用ConstraintLayout

标签: android android-layout android-progressbar


【解决方案1】:

使用约束布局

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

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgLeftArrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/abc_vector_test" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center_horizontal">

                <ImageView
                    android:id="@+id/imgGreendot"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/abc_vector_test" />

                <TextView
                    android:id="@+id/tvStatus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>


            <ImageView
                android:id="@+id/imgBid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvExpected"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tvTotalBid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total Bids | "
                android:textColor="#a9a9a9"
                android:textSize="14dp" />

            <TextView
                android:id="@+id/tvDeclined"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>


        <LinearLayout
            android:id="@+id/layoutswitch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_weight="1"
                android:text="Show Active Drivers Only" />
        </LinearLayout>

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar5"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:indeterminate="true"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="@+id/linearLayout3"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/linearLayout3" />


</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 在父布局不会更改为ConstrainLayoutRelativeLayout 之前它不会起作用
  • 对于重叠的布局,需要使用RelativeLayout或ConstraintLayout作为父布局。这个答案不起作用,因为父布局是 LinearLayout。
  • 即使您在progressBar 上使用wrap_content,它仍然在LinearLayout 内,并且宽度仍然被占用,不是由progressBar 而是它的外部LinearLayout,违背了目的。 @PratikButani 我认为除了正确使用FrameLayout 的 2 个选项(包装 rootLayout 和进度条)之外也可以工作
  • @Mithu 花时间了解有关 ConstraintLayout 和平面视图层次结构(无嵌套视图组)的更多信息。此答案中的布局不是最佳的,可以改进。
猜你喜欢
  • 2011-04-26
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2011-09-08
相关资源
最近更新 更多