2015-02-23 36 views
0

我創建了一個活動,其中包含一個AsyncTask以從互聯網獲取一些數據。我在佈局中添加了一個進度條,用戶無法與edittext交互,直到數據加載完成。 在內容加載過程中,我會使當前佈局較暗,以便用戶瞭解它在加載期間未處於活動狀態的佈局。 是否可以添加一個覆蓋當前包含進度條小部件的較暗佈局?如何在內容加載期間將較暗的佈局與當前佈局重疊?

這裏是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/home" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="20dp" 
      android:contentDescription="@string/mylogo" 
      android:src="@drawable/myapp_logo" />  

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

      <TextView 
       android:id="@+id/textMethod" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/method" 
       android:textColor="@color/black" /> 

      <Spinner 
       android:id="@+id/spinner1" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_below="@id/textSpinner" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/textMethod2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/method2" 
       android:textColor="@color/black" /> 

      <EditText 
       android:id="@+id/editMethod2" 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textMethod2" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 

      <ProgressBar 
       android:id="@+id/progressBar1" 
       style="?android:attr/progressBarStyleLarge" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/textTotal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/total" 
       android:textColor="@color/black" /> 

      <EditText 
       android:id="@+id/editTotal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textTotal" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 
     </RelativeLayout> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"     
      android:layout_marginTop="20dp" 
      android:layout_marginBottom="20dp" 
      android:background="@drawable/yellow_button_event" 
      android:onClick="send" 
      android:text="@string/send" /> 
    </LinearLayout> 
</ScrollView> 

回答

1

是的,你可以把一個全屏視圖較暗的背景和知名度=不見了。當您的應用程序正在加載時,您將更改爲visibility = true。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/home" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:orientation="vertical" > 

     <RelativeLayout 
       android:id="@+id/darkerView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#60000000" 
       android:visibility="gone" 
     /> 


     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="20dp" 
      android:contentDescription="@string/mylogo" 
      android:src="@drawable/myapp_logo" />  

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

      <TextView 
       android:id="@+id/textMethod" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/method" 
       android:textColor="@color/black" /> 

      <Spinner 
       android:id="@+id/spinner1" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_below="@id/textSpinner" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/textMethod2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/method2" 
       android:textColor="@color/black" /> 

      <EditText 
       android:id="@+id/editMethod2" 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textMethod2" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 

      <ProgressBar 
       android:id="@+id/progressBar1" 
       style="?android:attr/progressBarStyleLarge" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/textTotal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="@string/total" 
       android:textColor="@color/black" /> 

      <EditText 
       android:id="@+id/editTotal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textTotal" 
       android:textSize="18sp" 
       android:inputType="text" 
       android:textColor="@color/black" /> 
     </RelativeLayout> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"     
      android:layout_marginTop="20dp" 
      android:layout_marginBottom="20dp" 
      android:background="@drawable/yellow_button_event" 
      android:onClick="send" 
      android:text="@string/send" /> 
    </LinearLayout> 
</ScrollView> 

希望它可以幫助你!

+0

埃姆...什麼都沒有用你的編輯更改... – smartmouse 2015-02-23 17:18:27

1

很確定android.app.ProgressDialog是專門爲此設計的。我主要使用不確定模式(不顯示進度,只是微調),但是它的設置並不複雜。

ProgressDialog mProgressDialog = new ProgressDialog(context); 
    mProgressDialog.setMessage(message); 
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    mProgressDialog.setCancelable(false); 

然後從你的異步任務,可以

@Override 
protected void onProgressUpdate(Integer... values) { 
    //maintain a reference to your activity 
    ((MyActivity)activity).updateProgressDialog(value); 
} 

其中updateProgressDialog(int value)是說MyActivity。喜歡的東西

public void updateProgressDialog(int value){ 
    if (mProgressDialog!=null && mProgressDialog.isShowing()){ 
     mProgressDialog.setProgress(value); 
    } 
} 

編輯: 如果你在不使用ProgressDialog設置,然後亞歷杭德羅的方法效果。使用具有某種透明背景的視圖,並根據需要顯示和隱藏。您需要將clickable設置爲true以捕獲點擊,從而防止點擊。

<ImageView 
    android:id="@+id/overlay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@color/some_kinda_dark_transparent_color" 
    android:visibility="gone" 
    android:clickable="true"/> 
+0

我不使用ProgressDialog,我使用進度......如何達到同樣的結果呢? – smartmouse 2015-02-23 17:40:00

+1

那麼我猜Alejandro的方法。添加一個透明的src或背景視圖,並根據需要顯示和隱藏它。您需要將其設置爲可點擊才能捕捉點擊並阻止點擊。它不優雅,但它應該工作。 ' ' – 2015-02-23 20:49:59