我正在尋找在屏幕上顯示覆蓋圖,該屏幕顯示一個小的加載代碼或可能甚至一些文本,而我的應用程序嘗試登錄到服務器。我的登錄屏幕全部位於垂直線性佈局的內部。在Android屏幕上顯示加載覆蓋圖
我想要達到的效果是這樣的:http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message
我正在尋找在屏幕上顯示覆蓋圖,該屏幕顯示一個小的加載代碼或可能甚至一些文本,而我的應用程序嘗試登錄到服務器。我的登錄屏幕全部位於垂直線性佈局的內部。在Android屏幕上顯示加載覆蓋圖
我想要達到的效果是這樣的:http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message
也許爲時已晚,但我想有人會覺得它有用。
活動:
public class MainActivity extends Activity implements View.OnClickListener {
String myLog = "myLog";
AlphaAnimation inAnimation;
AlphaAnimation outAnimation;
FrameLayout progressBarHolder;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
new MyTask().execute();
break;
}
}
private class MyTask extends AsyncTask <Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
button.setEnabled(false);
inAnimation = new AlphaAnimation(0f, 1f);
inAnimation.setDuration(200);
progressBarHolder.setAnimation(inAnimation);
progressBarHolder.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
outAnimation = new AlphaAnimation(1f, 0f);
outAnimation.setDuration(200);
progressBarHolder.setAnimation(outAnimation);
progressBarHolder.setVisibility(View.GONE);
button.setEnabled(true);
}
@Override
protected Void doInBackground(Void... params) {
try {
for (int i = 0; i < 5; i++) {
Log.d(myLog, "Emulating some task.. Step " + i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
}
佈局的xml:
<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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start doing stuff"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Do Some Stuff"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
android:id="@+id/progressBarHolder"
android:animateLayoutChanges="true"
android:visibility="gone"
android:alpha="0.4"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center" />
</FrameLayout>
</RelativeLayout>
+1代表實際代碼 – JcKelley
這應該被選作答案。所選答案建議關於不再使用的ProgressDialog,並且不提供代碼。 –
我進度中相對佈局和隱藏或分別顯示它。而且活動可以是透明的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/hsvBackgroundContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
</LinearLayout>
<ProgressBar
android:id="@+id/pbProgess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
使用相對佈局而不是android用於登錄的標準佈局是我的問題的解決方案,我無法集中進度條。謝謝! – PeterPan
您必須使用AsyncTask之類的線程概念進行後臺操作。使用這個你可以隱藏UI部分的實際工作。在完成操作後,AsyncTask將會被取消分配。
使用ProgressDialog可以創建帶有應用程序消息的微調控件。雖然它沒有達到如圖所示的確切效果,但它是顯示應用程序正常工作的好方法。
我喜歡Kostya But's answer的辦法。
大廈,這裏的一對夫婦的想法,使同樣的覆蓋容易可重複使用的在您的應用程序:
考慮將疊加的FrameLayout在一個單獨的佈局文件,例如res/layout/include_progress_overlay
:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.4"
android:animateLayoutChanges="true"
android:background="@android:color/black"
android:clickable="true"
android:visibility="gone">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>
</FrameLayout>
(一件事,我在覆蓋的FrameLayout是android:clickable="true"
補充所以顯示的覆蓋,同時,它可以防止點擊經歷到它下面的UI元素至少在我的典型應用案例,這是我。想)
然後包括它需要的地方。
<!-- Progress bar overlay; shown while login is in progress -->
<include layout="@layout/include_progress_overlay"/>
而在代碼:
View progressOverlay;
[...]
progressOverlay = findViewById(R.id.progress_overlay);
[...]
// Show progress overlay (with animation):
AndroidUtils.animateView(progressOverlay, View.VISIBLE, 0.4f, 200);
[...]
// Hide it (with animation):
AndroidUtils.animateView(progressOverlay, View.GONE, 0, 200);
隨着動畫代碼提取到util的方法:
/**
* @param view View to animate
* @param toVisibility Visibility at the end of animation
* @param toAlpha Alpha at the end of animation
* @param duration Animation duration in ms
*/
public static void animateView(final View view, final int toVisibility, float toAlpha, int duration) {
boolean show = toVisibility == View.VISIBLE;
if (show) {
view.setAlpha(0);
}
view.setVisibility(View.VISIBLE);
view.animate()
.setDuration(duration)
.alpha(show ? toAlpha : 0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(toVisibility);
}
});
}
(這裏使用view.animate()
,在API 12中加入,而不是AlphaAnimation
。)
我正在使用ConstraintLayout,並且必須使用'progressOverlay.bringToFront()'來防止與其他UI元素的交互。否則,整潔的回答Jonik和Kostya。 – DJSquared
您正在尋找這個?對 ? http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android –
@ M-WaJeEh如何添加文字和/或圖形到該活動?像你添加到一個正常的「活動」, –
。它只是一個正常的「活動」,具有不同的主題。 –