【问题标题】:Progressbar not displaying when TextView has content当 TextView 有内容时进度条不显示
【发布时间】:2018-09-03 17:40:23
【问题描述】:

我正在使用一个扩展 Dialog 的类。它应该显示一个progressbar 元素。有时会有消息,有时没有。

progressbar在没有消息时显示,但在有消息时不显示,我不明白为什么。

这是布局代码:

<TextView
    android:id="@+id/dialog_message"
    style="@style/arial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="100dp"
    android:layout_marginRight="100dp"
    android:layout_marginBottom="20dp"
    android:text=""
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    />

<ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="100dp"
    android:layout_marginRight="100dp"
    android:layout_marginTop="10dp"
    android:visibility="gone" />

这里是java:

public void progressDialog () {    
    if(this.message.getText().length() > 0){
        message.setVisibility(View.VISIBLE);
    }else{
        message.setVisibility(View.GONE);
    }
    progress.setVisibility(View.VISIBLE);
    leftButton.setVisibility(View.GONE);
    rightButton.setVisibility(View.GONE);
}

message 设置为GONE 时,进度条显示,当message 设置为VISIBLE 时,进度条不显示。我认为它以某种方式被消息隐藏了。

我尝试在布局中移动它,因为这个问题可能暗示需要ProgressBar not displaying,但无济于事。

如何在调用progressDialog() 时始终显示进度条?

编辑***** 添加完整的布局 xml

<?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:background="@drawable/dialog_background"
  android:orientation="vertical">

<TextView
    android:id="@+id/dialog_title"
    style="@style/arialBold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="35dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="@string/waiting"
    android:textColor="@android:color/white"
    android:textSize="21sp" />

<TextView
    android:id="@+id/dialog_message"
    style="@style/arial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="30dp"
    android:text=" "
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

<ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="100dp"
    android:layout_marginRight="100dp"
    android:layout_marginTop="30dp"
    android:visibility="gone" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="30dp"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/dialog_button_one"
        style="@android:style/Widget.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@color/purple"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/dialog_button_one_text"
            style="@style/arialBold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"

android:layout_marginLeft="@dimen/alert_dialog_button_margin_left_right"

android:layout_marginRight="@dimen/alert_dialog_button_margin_left_right"
            android:layout_marginTop="10dp"
            android:ellipsize="end"
            android:maxLines="1"

            android:textColor="@android:color/white"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/dialog_button_two"
        style="@android:style/Widget.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="20dp"
        android:background="@color/purple"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/dialog_button_two_text"
            style="@style/arialBold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="@dimen/alert_dialog_button_margin_left_right"
            android:layout_marginRight="@dimen/alert_dialog_button_margin_left_right"
            android:layout_marginTop="10dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

    </LinearLayout>


 </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 我只是在调查约束布局@Zeeshan。我怀疑要解决这个问题,我需要根据约束模型重新设计整个布局,现在没有时间 - 这不是优先事项。如果有人(@Arsalan Khan、@Zeeshan)可以解释为什么这些替代布局模式是必要的,那么很乐意接受答案

标签: java android-linearlayout


【解决方案1】:

我认为您正在使用线性布局。所以使用相对布局而不是线性布局。

【讨论】:

  • 把整个显示弄乱了?我希望这些垂直线性显示,所以线性布局应该没问题。在 IDE 中,当添加所有元素可见的组件时,它们会很好地定位自己吗?
  • 您是否设置了对话框布局的固定高度?
  • 布局高度设置为 wrap_content,但是当他进度微调器不显示对话框时,它有空间。即它不会缩小窗口。 (我会在问题中添加完整的布局 xml)
【解决方案2】:

将父约束布局添加到线性布局并将进度条移出线性布局。

<constraint>
   <linear/>
   <progress/>
</constraint>

类似这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_background"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" 
    >


    <!-- Your complete layout -->

</LinearLayout>


    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:visibility="gone" />

</android.support.constraint.ConstraintLayout>

【讨论】:

  • 只是包装和移动进度是行不通的。请参阅我添加到原始问题的评论。谢谢。
【解决方案3】:

好的,看起来你使用了一些错误的属性,看看:

android:layout_gravity="center" 会将您的 TextView 放在布局的中心,但您也将它设置在您的 ProgressBar 中表示只会显示其中一个,仅当您想一次显示一个视图时才有用,如果您想同时显示两个,请尝试删除此属性之一。

还有一点,你在TextView上设置了android:layout_margin="30dp",这样你的progressBar就会远离TextView,这也可能导致ProgressBar不可见,试试16dp

<TextView
android:id="@+id/dialog_message"
style="@style/arial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" // remove here or in ProgressBar
android:layout_margin="16dp" // I just replace 30dp to 16dp
android:text=" "
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="20sp" />

编辑 - 另一个答案

我编辑您的完整 XML 代码,用填充替换一些边距,并始终显示进度条。您现在可以隐藏/显示 textView 或进度条,它应该可以工作了。

我也替换了一些属性,因为我这里没有,请确保再次添加。希望对你有帮助。

我建议使用 RelativeLayout 来改进您的 UI。下面我展示了当前的 XML 行为。

当显示进度条时

下面链接的完整源代码,我无法在此处粘贴代码。

https://gist.github.com/pedromassango/a6dc213823b297931f1ef2e6b6954466

【讨论】:

  • 这些听起来很有希望,但不幸的是没有奏效。我尝试依次消除每个元素上的重力,仍然没有显示微调器
  • 感谢您的坚持,但这仍然无法在我的代码中运行,一定有其他事情发生,我将不得不进一步调查
  • 要始终显示你的进度条,永远不要调用 progress.setVisibility(View.GONE);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2016-09-13
相关资源
最近更新 更多