在TextView更改后,ImageButton更改位置

时间:2018-05-11 10:36:18

标签: android android-layout

我的应用程序上有一个自定义webview,顶部有一个矩形栏,用于显示页面标题,后退图标和分享按钮。当页面加载时,我显示“正在加载...”,一旦完成,我就会显示页面标题。

问题是,当发生这种情况时,后退图标会从其位置移动或跳跃。我无法弄清楚原因。

While Loading

After Loading

下面是我的xml文件,仅用于吧。

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

    <ImageButton
        android:id="@+id/backIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginStart="10dp"
        android:layout_weight="1"
        android:adjustViewBounds="false"
        android:background="?android:selectableItemBackground"
        android:cropToPadding="false"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:scaleType="fitCenter"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/back_icon_small"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/articleLoading"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center_vertical|center_horizontal"
        android:maxEms="10"
        android:maxLines="1"
        android:text="TextView"
        android:textColor="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/backIcon" />

    <ImageButton
        android:id="@+id/shareButton"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginEnd="15dp"
        android:adjustViewBounds="false"
        android:background="?android:selectableItemBackground"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/button_share_white" />

    <TextView
        android:id="@+id/articleTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:maxEms="20"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="TextView"
        android:textColor="@color/colorAccent"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/shareButton"
        app:layout_constraintStart_toEndOf="@id/backIcon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.6" />

</android.support.constraint.ConstraintLayout>

以下是在完全加载页面时设置标题的方法。

@Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);

        progressBar.setProgress(newProgress);

        if (newProgress == 100) {
            articleTitle.setText(view.getTitle());
            progressBar.setVisibility(View.INVISIBLE);
            loading.setVisibility(View.INVISIBLE);
            articleTitle.setVisibility(View.VISIBLE);
        }
    }

3 个答案:

答案 0 :(得分:0)

使用FrameLayout而不是ConstraintLayout并将您的代码更改为此,您将获得所需的内容。

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/backIcon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginStart="10dp"
        android:adjustViewBounds="false"
        android:layout_gravity="center_vertical|start"
        android:background="?android:selectableItemBackground"
        android:cropToPadding="false"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/articleLoading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:textSize="12dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:gravity="center_vertical|center_horizontal"
        android:maxEms="10"
        android:maxLines="1"
        android:text="TextView"
        android:textColor="@color/colorAccent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="center_horizontal|center_vertical"/>

    <ImageButton
        android:id="@+id/shareButton"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginEnd="15dp"
        android:adjustViewBounds="false"
        android:layout_gravity="center_vertical|end"
        android:background="?android:selectableItemBackground"
        android:scaleType="fitCenter"/>

    <TextView
        android:id="@+id/articleTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:maxEms="20"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="TextView"
        android:textColor="@color/colorAccent"
        android:visibility="invisible" />
</FrameLayout>

答案 1 :(得分:0)

试试这个并设置你的背影..

let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.image", "public.audio", "public.movie", "public.text", "public.item", "public.content", "public.source-code"],
                                                                                          in: .import)

        if #available(iOS 11.0, *) {
            documentPicker.allowsMultipleSelection = true
        } else {
            // Fallback on earlier versions
        }

        self.present(documentPicker, animated: true,
                     completion: nil)

答案 2 :(得分:0)

Please make the changes in constraints as below 

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">

<ImageButton
    android:id="@+id/backIcon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="false"
    android:background="?android:selectableItemBackground"
    android:cropToPadding="false"
    android:gravity="center_vertical"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/back_icon_small"/>

<TextView
    android:id="@+id/articleLoading"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:ellipsize="end"
    android:gravity="center_vertical|center_horizontal"
    android:maxEms="10"
    android:maxLines="1"
    android:text="Loding"
    android:textColor="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>


<TextView
    android:id="@+id/articleTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:maxEms="20"
    android:maxLines="1"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:text="TextView"
    android:textColor="@color/colorAccent"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/shareButton"
    app:layout_constraintStart_toEndOf="@+id/backIcon"
    app:layout_constraintTop_toTopOf="parent"/>

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/backIcon"/>

<ImageButton
    android:id="@+id/shareButton"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_marginEnd="15dp"
    android:adjustViewBounds="false"
    android:background="?android:selectableItemBackground"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/button_share_white"/>