ImageButton меняет положение после изменения TextView - PullRequest
0 голосов
/ 11 мая 2018

У меня есть собственное веб-представление в моем приложении, в верхней части которого находится прямоугольная полоса для отображения заголовка страницы, значка возврата и кнопки «Поделиться». Когда страница загружается, я показываю «Загрузка ...» и, как только она завершается, я отображаю заголовок страницы.

Проблема в том, что когда это происходит, иконка спины перемещается или подпрыгивает с места. Я не могу понять, почему.

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);
        }
    }

Ответы [ 4 ]

0 голосов
/ 11 мая 2018
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"/>

0 голосов
/ 11 мая 2018

используйте 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>
0 голосов
/ 11 мая 2018

Попробуйте это и установите ваше заднее изображение ..

    <ImageButton
    android:id="@+id/backIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:src="@android:drawable/arrow_up_float"
    android:layout_marginLeft="10dp" />
0 голосов
/ 11 мая 2018

Установите нижние и верхние ограничения на родительский, как вы делаете с другими компонентами. Это должно решить это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...