简体   繁体   中英

Android ProgressBar is not aligning in center of layout

I am using a progress bar inside drawer layout. When I start the app the progress bar is always aligned to top left corner of screen. I tried to set android:layout_gravity property but still It is shown in top left corner. Below is my xml layout file.

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"    >

        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:visibility="gone"
            style="?android:attr/progressBarStyleLarge" />

        <ListView 
            android:id="@+id/mainListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </FrameLayout>

    <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:dividerHeight="0dp"
            android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>

use android:gravity="<whatever>" on the FrameLayout , since you only have the progressbar inside it.
Also, your drawer is going to be the listview because that's what has the layout_gravity="start" property set. If you want the progress bar to be within the left-drawer you can wrap the progress bar and listview into a container and set the layout_gravity="start" on that.
without further explanation, that's all i can aid with.

It's work for me. In FrameLayout set

android:layout_centerInParent="true"

Please try this code, This code is work for me.

ProgressBarView.xml

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_gravity="center"
    android:orientation="vertical">

</ProgressBar>

Now include this layout into NavigationView,

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">
      <include layout="@layout/progress_bar"/>
</android.support.design.widget.NavigationView>

Preview in Studio

代码预览。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM