内容区的进度条
Progress bar in the content area
我使用 this "method" 创建进度条。它一直有效,直到我不使用片段。
现在我使用片段、material drawer 和 WebView(当然在片段内)。我创建了进度条,但它显示在屏幕顶部。
这是我的代码:
进度条:
progressBarLoad = new ProgressBar(getActivity().getBaseContext(), null, android.R.attr.progressBarStyleHorizontal);
progressBarLoad.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 24));
progressBarLoad.setProgress(100);
progressBarLoad.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_light));
FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
decorView.addView(progressBarLoad);
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
progressBarLoad.setY(Main.getActionBarSize() + (float) Math.ceil(25 * getResources().getDisplayMetrics().density) - 10);
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.removeOnGlobalLayoutListener(this);
}
});
主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main.Main">
<android.support.v7.widget.Toolbar
android:id="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green"
app:popupTheme="@style/Theme.AppCompat"
app:theme="@style/ToolbarTheme" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout" />
</LinearLayout>
最后是 webView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SwipeContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/WebViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
截图如下:
所以,问题是,如何将这个进度条添加到内容屏幕中?不是整个设备屏幕。
添加片段后,尝试将进度条添加到 activity 的框架布局而不是装饰视图。
你可以把它放在XML中,这是最简单的方法。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main.Main">
<android.support.v7.widget.Toolbar
android:id="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green"
app:popupTheme="@style/Theme.AppCompat"
app:theme="@style/ToolbarTheme"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_below="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_below="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout"/>
</RelativeLayout>
或者您可以将进度条放在您扩充的布局中。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_below="@+id/progressBar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SwipeContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/WebViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
所以,首先感谢大家的回答。我尝试了很多东西,搜索了很多,但我一无所获。然后...突然我想起如果我在相对布局中将一些 object/layout 放在下面,它们将位于屏幕的顶部。所以我一开始就做错了。我把进度条放在相对布局的顶部 --> 这就是我没看到的原因。所以我把相对布局放在底部,woala!它有效...但等待不正确..
为什么?因为我再次添加了工具栏大小,当然还有 prog。栏距工具栏约 40px。所以我想通了,现在使用这段代码它可以在任何屏幕上工作:
progressBarLoad = (ProgressBar) view.findViewById(R.id.progressBar);
progressBarLoad.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 24));
progressBarLoad.setProgress(100);
progressBarLoad.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_light));
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//THIS IS IMPORTANT
progressBarLoad.setY(((float) Math.ceil(12.5*getResources().getDisplayMetrics().density)-38));
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.removeOnGlobalLayoutListener(this);
}
});
我使用 this "method" 创建进度条。它一直有效,直到我不使用片段。
现在我使用片段、material drawer 和 WebView(当然在片段内)。我创建了进度条,但它显示在屏幕顶部。
这是我的代码:
进度条:
progressBarLoad = new ProgressBar(getActivity().getBaseContext(), null, android.R.attr.progressBarStyleHorizontal);
progressBarLoad.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 24));
progressBarLoad.setProgress(100);
progressBarLoad.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_light));
FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
decorView.addView(progressBarLoad);
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
progressBarLoad.setY(Main.getActionBarSize() + (float) Math.ceil(25 * getResources().getDisplayMetrics().density) - 10);
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.removeOnGlobalLayoutListener(this);
}
});
主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main.Main">
<android.support.v7.widget.Toolbar
android:id="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green"
app:popupTheme="@style/Theme.AppCompat"
app:theme="@style/ToolbarTheme" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout" />
</LinearLayout>
最后是 webView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SwipeContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/WebViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
截图如下:
所以,问题是,如何将这个进度条添加到内容屏幕中?不是整个设备屏幕。
添加片段后,尝试将进度条添加到 activity 的框架布局而不是装饰视图。
你可以把它放在XML中,这是最简单的方法。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main.Main">
<android.support.v7.widget.Toolbar
android:id="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green"
app:popupTheme="@style/Theme.AppCompat"
app:theme="@style/ToolbarTheme"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_below="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_below="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout"/>
</RelativeLayout>
或者您可以将进度条放在您扩充的布局中。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_below="@+id/progressBar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SwipeContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/WebViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
所以,首先感谢大家的回答。我尝试了很多东西,搜索了很多,但我一无所获。然后...突然我想起如果我在相对布局中将一些 object/layout 放在下面,它们将位于屏幕的顶部。所以我一开始就做错了。我把进度条放在相对布局的顶部 --> 这就是我没看到的原因。所以我把相对布局放在底部,woala!它有效...但等待不正确..
为什么?因为我再次添加了工具栏大小,当然还有 prog。栏距工具栏约 40px。所以我想通了,现在使用这段代码它可以在任何屏幕上工作:
progressBarLoad = (ProgressBar) view.findViewById(R.id.progressBar);
progressBarLoad.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 24));
progressBarLoad.setProgress(100);
progressBarLoad.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_light));
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//THIS IS IMPORTANT
progressBarLoad.setY(((float) Math.ceil(12.5*getResources().getDisplayMetrics().density)-38));
ViewTreeObserver observer = progressBarLoad.getViewTreeObserver();
observer.removeOnGlobalLayoutListener(this);
}
});