【问题标题】:Trouble with progressbar进度条有问题
【发布时间】:2014-10-22 20:15:50
【问题描述】:

所以我有一个奇怪的问题。我的布局看起来像这样:

<LinearLayout      
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:id="@+id/myText" />

     <ProgressBar
        android:id="@+id/progressBarHorizontal"            
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

看起来很简单,但我的应用程序没有启动。我没有错误,但应用程序无法启动。但是如果我从布局应用程序中删除标签成功启动。 我不明白我错在哪里。有人可以帮助我吗?

【问题讨论】:

  • 进度条样式设置了吗?我不知道默认情况下它的行为...style="@android:style/Widget.ProgressBar.Horizontal"
  • 尝试将maxprogress 属性添加到ProgressBar 标记

标签: java android xml layout progress-bar


【解决方案1】:

这个布局不能工作,因为你没有设置LinearLayout的layout_width和layout_height属性。 尝试添加

android:layout_width="wrap_content"
android:layout_height="wrap_content"

到您的 LinearLayout。

正如here: (Layouts: Layout Parameters)所说的

所有视图组都包含一个宽度和高度(layout_width 和 layout_height),每个视图都需要定义它们。

【讨论】:

    【解决方案2】:

    您的问题不一定与您的布局有关(虽然您缺少必需的属性,但您的 IDE 可能会为您填写这些属性),而是与 ProgressBar 的定义有关。

    您缺少必要的样式属性,该属性告诉框架要呈现哪个小部件。

    如果你在布局中使用它,产生的效果(如果它甚至开始)是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/my_text"
                />
        <ProgressBar
                android:layout_width="209dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar"/>
    </LinearLayout>
    

    结果:

    如果你像下面的代码那样定义样式属性,你的结果会好很多:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/my_text"
                />
        <ProgressBar
                style="@android:style/Widget.ProgressBar.Horizontal"
                android:layout_width="209dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar" android:progress="35"/>          
    </LinearLayout>
    

    结果:

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2014-06-11
      • 2016-07-30
      相关资源
      最近更新 更多