2015-02-23 38 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