0

如何正确防止 ProgressBar 与最右侧的视图重叠?请注意,我不想使用 TableLayout。我也已经阅读了这个优秀的 SOF链接

ProgressBar_overlap

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >

<RelativeLayout
    android:id="@+id/rel_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="LLL" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/RIGHT"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="RRR" />
</RelativeLayout>

<ProgressBar
    android:id="@+id/pb"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="5sp"
    android:layout_toRightOf="@+id/rel_left"
    android:max="100"
    android:progress="80" />

</RelativeLayout>
4

2 回答 2

2

这应该可以解决问题:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
    <!-- Align parent left cause this textview to be on the left! -->
    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="LLL" />

    <!-- Align parent right cause this textview to be on the right! -->
    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="RRR" />

    <!-- Now we want this progressbar to take up all the space in between. Just specify the 'layout_toLeftOf' and 'layout_toRightOf' properites! -->
    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="5sp"
        android:layout_toRightOf="@+id/left"
        android:layout_toLeftOf="@+id/right"
        android:max="100"
        android:progress="80" />

</RelativeLayout>

我不确定你为什么将 textviews 包装在它们自己的 RelativeLayout 中,我认为这可能只是你的一个误解,因为你可以强制 textviews 本身位于左侧和右侧

于 2012-05-20T03:05:10.637 回答
1

如果您在 RelativeLayout 中有 5 个项目,这是我的解决方案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >

<!-- Align parent left cause this TextView to be on the left! -->

<TextView
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="LLL" />

<TextView
    android:id="@+id/left_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_toRightOf="@+id/left"
    android:text="100" />

<!-- Align parent right cause this TextView to be on the right! -->

<TextView
    android:id="@+id/right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="RRR" />

<TextView
    android:id="@+id/right_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5sp"
    android:layout_toLeftOf="@+id/right"
    android:text="2" />

<!-- Now we want this ProgressBar to take up all the space in between. Just specify the 'layout_toLeftOf' and 'layout_toRightOf' properites! -->

<ProgressBar
    android:id="@+id/pb"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="5sp"
    android:layout_toLeftOf="@+id/right_number"
    android:layout_toRightOf="@+id/left_number"
    android:max="100"
    android:paddingLeft="5sp"
    android:paddingRight="5sp"
    android:progress="80" />

</RelativeLayout>
于 2012-05-20T20:43:56.253 回答