在哪里以及如何在现有布局上放置进度条?
Where and how to put a progressbar on a existing layout?
我创建了一个 activity,其中包含一个用于从互联网获取 json 的 AsyncTask。我在现有布局中添加了一个进度条,以便用户在数据下载完成之前无法与 EditText 进行交互。
在哪里将进度条小部件放在现有布局上?
如何让用户在加载进度条时不点击现有的小部件(EditText、Spinner 等)?
这是我的布局:
<?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>
我在 Eclipse 中使用可视化 xml 编辑器将进度条添加到屏幕中央,但正如您从代码中看到的那样,它将进度条放在现有的 RelativeLayout 中,位于 EditText 下方。这导致在显示进度条期间,更近的小部件的位置发生变化。如何避免这种情况?在这种情况下,在现有布局的中间和上方添加进度条的最佳方法是什么?
我希望你能帮助我,请原谅我的英语。
// Initialzing Progress Dialog
ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
上面的代码将显示消息 "Loading..." 的良好进展
并在 AsyncTask 的 onResult
中隐藏进度条
pDialog.hide();
此进度将冻结 UI,直到调用 hide() 为止。这意味着用户在收到响应之前不能做任何事情。
pDialog 中还有一些很酷的方法可以根据您的喜好自定义进度条。
您可以在相对布局中定义进度条和滚动视图。当你想显示它时更新进度条的可见性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
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_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >
your layout content here.....
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/progressBarlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</RelativeLayout>
我创建了一个 activity,其中包含一个用于从互联网获取 json 的 AsyncTask。我在现有布局中添加了一个进度条,以便用户在数据下载完成之前无法与 EditText 进行交互。 在哪里将进度条小部件放在现有布局上? 如何让用户在加载进度条时不点击现有的小部件(EditText、Spinner 等)?
这是我的布局:
<?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>
我在 Eclipse 中使用可视化 xml 编辑器将进度条添加到屏幕中央,但正如您从代码中看到的那样,它将进度条放在现有的 RelativeLayout 中,位于 EditText 下方。这导致在显示进度条期间,更近的小部件的位置发生变化。如何避免这种情况?在这种情况下,在现有布局的中间和上方添加进度条的最佳方法是什么?
我希望你能帮助我,请原谅我的英语。
// Initialzing Progress Dialog
ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
上面的代码将显示消息 "Loading..." 的良好进展 并在 AsyncTask 的 onResult
中隐藏进度条 pDialog.hide();
此进度将冻结 UI,直到调用 hide() 为止。这意味着用户在收到响应之前不能做任何事情。
pDialog 中还有一些很酷的方法可以根据您的喜好自定义进度条。
您可以在相对布局中定义进度条和滚动视图。当你想显示它时更新进度条的可见性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
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_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >
your layout content here.....
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/progressBarlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</RelativeLayout>