【问题标题】:Same aspect ratio of progressbar in custom listview自定义列表视图中进度条的相同纵横比
【发布时间】:2012-07-18 14:07:26
【问题描述】:

我有一个带有自定义行布局的列表视图。在这个自定义行中是一个进度条(“圆圈”),它指示加载状态。为了使布局看起来像我喜欢的那样,我使用 layout_weight 作为进度条和包含其他 gui 内容的布局。

例子:

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="1" />


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="7"
    android:layout_marginLeft="6dip" 
    android:orientation="vertical"
    >

我希望进度条具有相同的纵横比。

我的问题是进度条在不同设备上看起来很奇怪。在我的 Galaxy Nexus 上,一切看起来都很好,并且进度条具有相同的纵横比。但是在 - 例如 - 我的模拟器上,进度条看起来像一个鸡蛋,这太糟糕了。

谁能指出我的错误?

编辑:

我不使用 wrap_content,因为布局将在视图的其余部分使用进度条的宽度,这真的很小(然后就没有文本的地方了)。

编辑2:

两张图片显示问题:

“蛋” http://s14.directupload.net/file/d/2955/89xvdhrz_png.htm

wrap_content 的使用 http://s1.directupload.net/file/d/2955/sryrhkkk_png.htm

编辑3:

CFlex 的方法。整个 xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal">

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="center"
    android:layout_margin="4dp"/>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="7"
    android:layout_marginLeft="6dip" 
    android:orientation="vertical"
    >

    <TextView 
        android:id="@android:id/text1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <TextView 
        android:id="@android:id/text2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>

【问题讨论】:

  • 为什么不使用包装内容而不是在进度条中提供 match_parent 和 0dip
  • 编辑了我的问题。不能使用 wrap_content 因为我的布局会被破坏。
  • 您可以将样式设置为进度条,如小、正常和大。例如style="?android:attr/progressBarStyleSmall"
  • 谢谢,我试过了。没有“progressBarStyleNormal”资源。小,高度用于整行布局(见图二),太小了。大它看起来也很糟糕。也许我对这种布局的方法是完全错误的?
  • 如果您使用 LinearLayout 代替进度条 - 具有所需的尺寸 - 然后将带有 wrap_content 的进度条放置在该布局内?我认为它应该可以解决问题:布局的显式尺寸将确保您的布局的其余部分不会被切断,进度条上的wrap_content 将确保它不会失真。

标签: android android-layout progress-bar


【解决方案1】:

你为什么不试试这样的:

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content" 
    android:layout_height="match_parent"  
    android:layout_gravity="center"
    android:layout_margin="4dp"/>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:layout_marginLeft="6dip" 
    android:orientation="vertical"
    >

假设你在水平方向LinearLayout

如果这不起作用,请告诉我。

编辑:使用相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

    <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="4dp"/>

    <TextView
            android:id="@android:id/text1"
            android:layout_toRightOf="@+id/progressBar1"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

    <TextView
            android:layout_toRightOf="@+id/progressBar1"
            android:layout_below="@android:id/text1"
            android:id="@android:id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

</RelativeLayout>

【讨论】:

  • @eftokay83 确实,这很奇怪。你能发布你的整个xml吗?
  • @eftokay83 您是否尝试使用 android:layout_width="match_parent" 作为水平线性布局?
  • 是的,如果我为第二个布局设置它,结果是一样的。如果我为进度条设置它,结果是进度条会占据所有可用空间。然后屏幕上除了进度条之外什么都没有:) 哦,我认为我混合 fill_parent 和 match_parent 很脏,但它是一样的。
  • @eftokay83 我用 relativeLayout 编辑了我的代码。告诉我是否可以
  • 解决了问题!谢谢!!但仍然不完美-您知道如何以相同的纵横比拉伸进度条吗?但是非常感谢@CFlex!
猜你喜欢
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
相关资源
最近更新 更多