在哪里以及如何在现有布局上放置进度条?
我创建了一个活动,其中包含一个AsyncTask用于从互联网获取json。我在现有布局中添加一个进度条,以便用户在完成数据下载之前无法与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下面。这导致在进度条显示过程中更接近小部件的位置发生变化。如何避免这种情况?在这种情况下,在中间和现有布局中添加进度条的最佳方法是什么?
我希望你能帮助我,请原谅我的英语。
您可以定义您的进度条并以相对布局滚动视图。当你想要显示进度条时更新进度条的可见性。
<?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>
// Initialzing Progress Dialog
ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
上面的代码会显示与消息的良好进展“载入中...” 和隐藏在你的AsyncTask的onResult
pDialog.hide();
这一进展进度条将冻结UI,直到兽皮( ) 叫做。这意味着用户无法做任何事情直到收到响应。
并且在pDialog中还有一些很酷的方法可以根据您的喜好自定义进度条。
我忘了说,我使用进度,而不是ProgressDialog ... – smartmouse 2015-02-24 18:41:32
喔,好了,所以,这可能会帮助你http://www.androidhive.info/2012/04/android-downloading -file-by-showing-progress-bar/ – theapache64 2015-02-24 18:42:56
对不起,我的意思是你可以通过eclipse中的visual xml编辑器拾取ProgressBar小部件,就像这样:http://i.stack.imgur.com/SuGXK.png Now它有点平方... – smartmouse 2015-02-24 21:46:26
它的工作,但EditTexts和微调仍然可点击...如何避免该用户可以在AsyncTask完成前输入输入? – smartmouse 2015-02-24 21:55:24
我已根据您的要求更新了xml。将null作为点击侦听器添加到代码中的progressBarlayout(如findViewById(R.id.progressBarlayout).setOnClickListener(null))。 – 2015-02-25 16:58:20
作品!非常感谢! – smartmouse 2015-02-25 22:02:09