【问题标题】:Keeping ProgressBar from overlapping the right-most view in RelativeLayout防止 ProgressBar 与 RelativeLayout 中最右侧的视图重叠
【发布时间】:2012-05-20 02:38:41
【问题描述】:

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

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

【问题讨论】:

    标签: android progress-bar android-relativelayout overlap


    【解决方案1】:

    这应该可以解决问题:

    <?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 本身位于左右

    【讨论】:

    • 这行得通!为了回答您的问题,我将它们包装在自己的 RelativeLayout 中,因为我要在“rel_left”和“RIGHT”中添加另一个 TextView。
    【解决方案2】:

    如果您在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2020-05-09
      • 2019-02-14
      相关资源
      最近更新 更多