簡體   English   中英

如何在屏幕中央設置android進度欄

[英]how to set android progress bar in center of the screen

我正在嘗試在屏幕中央顯示進度條,但它沒有顯示在中央。 下面是我的布局。 我在這里做錯了什么? 基本上,這是一個在頂部顯示圖像和android gallery小部件的布局。 當用戶單擊縮略圖時,我正在從Web加載圖像,並且希望在加載圖像時顯示進度對話框。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/container" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

    <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="top" />

    <ImageView android:id="@+id/actualimage" android:scaleType="fitCenter"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:visibility="gone" android:layout_gravity="center_vertical|center_horizontal|center"
        android:adjustViewBounds="true" />

    <ProgressBar android:id="@+android:id/progress_small1"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:visibility="visible" android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal" />

</LinearLayout>

使用LinearLayout將垂直堆疊您的視圖。 現在,您的ImageViewProgressBar正在爭取居中位置。 對於這種情況,我絕對會推薦RelativeLayout

通過RelativeLayout ,您可以使用android:layout_centerInParent="true"

在根LinearLayout執行android:gravity="center" 設置android:visibility從分開的所有其他元素的ProgressBargone ,當你想要顯示的ProgressBar

添加以下內容:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

也許您應該進入進度對話框,因為我相信您正在嘗試顯示進度指示器,直到為圖庫加載圖像為止。

這將使您入門。

http://www.helloandroid.com/tutorials/using-threads-and-progress對話框

http://www.androidpeople.com/android-progress-dialog-example

而且,如果您正在尋找一種利用進度條本身的方法,那么也許您應該嘗試一下Bradley Uffner提供的答案

嘗試將進度條的layout_weight設置為1

我知道這個問題已經解決,但是考慮到布局視圖/子視圖的性能,RelativeLayout昂貴(需要兩次通過)。 如果您的視圖層次結構很復雜,請使用“相對布局”,或者如果您的視圖只有以下簡單視圖,請使用“線性布局/框架”布局。

根據我的經驗,當我的應用程序增長時,相對布局的UI性能會變差,最終會在所有布局文件上進行重新加工:(以下是將視圖(在我的情況下為ProgressView)放置在視圖中心的示例。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">
    <android.support.v7.widget.AppCompatImageView android:layout_gravity="center"  android:layout_width="match_parent"
        android:layout_height="0dp" app:srcCompat="@drawable/check" android:scaleType="fitCenter"
        android:layout_weight=".7"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" android:layout_weight=".3">
        <ProgressBar android:layout_gravity="center|center_horizontal" android:indeterminateTint="@color/textColorPrimary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:indeterminate="true"/>
    </FrameLayout>

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM