在视图的中心显示进度条

时间:2017-05-24 19:07:40

标签: android

我有以下xml,并希望在视图的中心显示progressbar。它显示在中心,但我无法看到其他相对布局中的其他内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
      <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
     </ProgressBar>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">
          <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="HI" />
    </RelativeLayout>


    <View
      android:layout_width="2dp"
      android:layout_height="match_parent"
      android:background="@color/separator" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">
           <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="HI" />
    </RelativeLayout>

 </LinearLayout> 

2 个答案:

答案 0 :(得分:1)

你应该这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HI" />
    </RelativeLayout>


    <View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="@color/separator" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HI" />
    </RelativeLayout>

</LinearLayout>

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />
</RelativeLayout>

答案 1 :(得分:1)

如果您想将进度条放在当前布局的中心,您可以执行以下操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">




    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HI" />
    </RelativeLayout>
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HI" />
    </RelativeLayout>

</LinearLayout>
此进度条中的

在两个视图之间的中心占据单独的空间 如果您想要进度栏水平中心更改 LinearLayout 重力,如下所示

android:gravity="center_horizontal"

或者你想在所有视图之上找到进度条试试@ Jonathan Aste 建议回答

相关问题