如何在Android

时间:2018-11-15 06:29:37

标签: android android-layout

这是我的xml,我想将一个布局保留在另一个布局上。

我有进度栏,然后在同一位置以编程方式执行可见和不可见的Textview和Progressbar的位置再次尝试Textview,因此我想将Progressbar保持在“重试”按钮上方,但是我无法执行此操作。

我从Google上搜索到可以使用框架布局进行操作,但无法正常工作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tesco_blue"
    android:paddingBottom="@dimen/margin_18"
    android:paddingLeft="@dimen/margin_18"
    android:paddingRight="@dimen/margin_12"
    android:orientation="vertical"
    android:paddingTop="@dimen/margin_18">
    <LinearLayout
        android:id="@+id/securebarcode_error_layout_variant_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:layout_marginLeft="@dimen/margin_56"
        android:layout_marginRight="@dimen/margin_56"
        android:visibility="visible"
        tools:visibility="visible">

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_40" />

        <TextView
            android:id="@+id/errortitle"
            style="@style/Typeface.Body.Bold.White"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:lineSpacingExtra="4sp"
            tools:text="@string/pwp_network_error" />


        <TextView
            android:id="@+id/errordesc"
            style="@style/Typeface.Body.White"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:layout_marginTop="@dimen/margin_12"
            android:lineSpacingExtra="8sp"
            tools:text="@string/pwp_network_error_description" />
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            >
        <ProgressBar
            android:id="@+id/pwp_progressbarlayout_variant_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"


            android:visibility="visible"
            android:paddingLeft="@dimen/cl_48"
            android:paddingRight="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingBottom="@dimen/padding_12"
            android:indeterminateDrawable="@drawable/custom_progress_bar"/>
        </FrameLayout>

        <TextView
            android:id="@+id/try_again"
            style="@style/Typeface.Body.Bold.HighlightBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/error_button_background"
            android:gravity="center"
            android:paddingLeft="@dimen/cl_48"
            android:paddingRight="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingBottom="@dimen/padding_12"
            android:text="@string/Tryagain"
            android:textColor="@color/fdv_pending_state_color" />


        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_40" />


    </LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

  1. 您可以在您的案例中简单地使用约束布局,因为基本上 从框架布局系列中,您可以实现以下代码。只需将根布局作为ConstraintLayout

      

    <TextView
        android:id="@+id/try_again"
        style="@style/Typeface.Body.Bold.HighlightBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@drawable/error_button_background"
        android:gravity="center"
        android:paddingLeft="@dimen/cl_48"
        android:paddingRight="@dimen/cl_48"
        android:paddingTop="@dimen/padding_12"
        android:paddingBottom="@dimen/padding_12"
        android:text="@string/Tryagain"
        android:textColor="@color/fdv_pending_state_color" />
    
    <ProgressBar
        android:id="@+id/pwp_progressbarlayout_variant_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/try_again"
        app:layout_constraintBottom_toBottomOf="@id/try_again"
        app:layout_constraintLeft_toLeftOf="@id/try_again"
        app:layout_constraintRight_toRightOf="@id/try_again"
        android:visibility="visible"
        android:paddingLeft="@dimen/cl_48"
        android:paddingRight="@dimen/cl_48"
        android:paddingTop="@dimen/padding_12"
        android:paddingBottom="@dimen/padding_12"
        android:indeterminateDrawable="@drawable/custom_progress_bar"/>
    

答案 1 :(得分:0)

如下更新您的布局-Relative Layout可以解决问题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tesco_blue"
    android:paddingBottom="@dimen/margin_18"
    android:paddingLeft="@dimen/margin_18"
    android:paddingRight="@dimen/margin_12"
    android:orientation="vertical"
    android:paddingTop="@dimen/margin_18">


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


        <LinearLayout
            android:id="@+id/securebarcode_error_layout_variant_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:layout_marginLeft="@dimen/margin_56"
            android:layout_marginRight="@dimen/margin_56"
            android:visibility="visible"
            tools:visibility="visible">

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_40" />

            <TextView
                android:id="@+id/errortitle"
                style="@style/Typeface.Body.Bold.White"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:lineSpacingExtra="4sp"
                tools:text="@string/pwp_network_error" />


            <TextView
                android:id="@+id/errordesc"
                style="@style/Typeface.Body.White"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center"
                android:layout_marginTop="@dimen/margin_12"
                android:lineSpacingExtra="8sp"
                tools:text="@string/pwp_network_error_description" />


            <TextView
                android:id="@+id/try_again"
                style="@style/Typeface.Body.Bold.HighlightBlue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/error_button_background"
                android:gravity="center"
                android:paddingLeft="@dimen/cl_48"
                android:paddingRight="@dimen/cl_48"
                android:paddingTop="@dimen/padding_12"
                android:paddingBottom="@dimen/padding_12"
                android:text="@string/Tryagain"
                android:textColor="@color/fdv_pending_state_color" />


            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_40" />


        </LinearLayout>

            <ProgressBar
                android:id="@+id/pwp_progressbarlayout_variant_a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_centerInParent="true"
                android:visibility="visible"
                android:indeterminateDrawable="@drawable/custom_progress_bar"/>


    </RelativeLayout>

</LinearLayout>

答案 2 :(得分:0)

您必须将try_again TextView移至FrameLayout的内部,以便进度栏将显示在TextView上

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tesco_blue"
android:orientation="vertical"
android:paddingLeft="@dimen/margin_18"
android:paddingTop="@dimen/margin_18"
android:paddingRight="@dimen/margin_12"
android:paddingBottom="@dimen/margin_18">

<LinearLayout
    android:id="@+id/securebarcode_error_layout_variant_a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/margin_56"
    android:layout_marginRight="@dimen/margin_56"
    android:orientation="vertical"
    android:visibility="visible"
    tools:visibility="visible">

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_40" />

    <TextView
        android:id="@+id/errortitle"
        style="@style/Typeface.Body.Bold.White"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:lineSpacingExtra="4sp"
        tools:text="@string/pwp_network_error" />


    <TextView
        android:id="@+id/errordesc"
        style="@style/Typeface.Body.White"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/margin_12"
        android:gravity="center"
        android:lineSpacingExtra="8sp"
        tools:text="@string/pwp_network_error_description" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">

        <ProgressBar
            android:id="@+id/pwp_progressbarlayout_variant_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateDrawable="@drawable/custom_progress_bar"
            android:paddingLeft="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingRight="@dimen/cl_48"
            android:paddingBottom="@dimen/padding_12"
            android:visibility="visible" />


        <TextView
            android:id="@+id/try_again"
            style="@style/Typeface.Body.Bold.HighlightBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/error_button_background"
            android:gravity="center"
            android:paddingLeft="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingRight="@dimen/cl_48"
            android:paddingBottom="@dimen/padding_12"
            android:text="@string/Tryagain"
            android:textColor="@color/fdv_pending_state_color" />
    </FrameLayout>


    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_40" />

这是您想要的图像。

enter image description here