进度条问题

时间:2014-10-22 20:15:51

标签: java android xml layout progress-bar

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

<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>

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

2 个答案:

答案 0 :(得分:0)

这种布局无法正常工作,因为您没有设置LinearLayout的layout_width和layout_height属性。 尝试添加

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

到你的LinearLayout。

如上所述here: (Layouts: Layout Parameters)

  

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

答案 1 :(得分:0)

您的问题不一定与您的布局有关(当您错过了必需的属性时,您可能会在IDE中为您填写这些属性),它&#39 ; s带有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>

结果: Sadface

如果您像下面的代码一样定义样式属性,那么结果会更好:

<?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>

结果: enter image description here

相关问题