androidx.constraintlayout.widget.ConstraintLayout:ProgressBar不在MaterialButton上方显示

时间:2019-03-10 13:26:21

标签: android android-constraintlayout

Android Studio 3.2.1

在我的build.gradle中:

android {
    dataBinding {
        enabled = true
    }
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 6
        versionName "0.0.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>


    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/jsonViewToolBar"
            layout="@layout/tool_bar"
            android:title='@{@string/add_trader}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/baseTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/default_margin"
            android:layout_marginEnd="8dp"
            android:text="@string/base"
            android:textSize="13sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jsonViewToolBar" />

        <EditText
            android:id="@+id/baseEditText"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/baseTextView" />

        <TextView
            android:id="@+id/quoteTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_margin"
            android:text="@string/quote"
            android:textSize="13sp"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/baseEditText" />

        <EditText
            android:id="@+id/quoteEditText"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/quoteTextView" />


        <com.google.android.material.button.MaterialButton
            android:id="@+id/startButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/min_height"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginBottom="@dimen/default_margin"
            android:gravity="center"
            android:textAllCaps="true"
            android:textSize="13sp"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/quoteEditText" />

        <include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

此处 progress_bar_layout

<?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:id="@+id/containerProgressBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4777"
    android:clickable="true"
    android:focusable="true">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="@dimen/min_height"
        android:layout_height="@dimen/min_height"
        android:elevation="10dp"
        android:indeterminateTint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果如下:

enter image description here

为什么ProgressBar UNDER MaterialButton吗?

2 个答案:

答案 0 :(得分:1)

要制作按钮,未提升的按钮,可以添加如下样式属性:-

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

答案 1 :(得分:1)

您已将进度条的标高设置为10dp,这是正确的答案,但是,因为进度条已嵌入ConstraintLayout中,并且包含了ConstraintLayout在另一种布局中,ConstraintLayout需要海拔。

android:elevation="10dp"ProgressBar移至其封闭的ConstraintLayout