在Android上,如何将子视图集中在其父视图中?
我花了很长时间在安卓系统的父视图中集中一个子视图(免责声明:我是一个在安卓系统上几乎没有经验的iOS开发人员)。我想要的非常简单:将一个不确定的进度视图(微调器)放在它的web视图父视图的中心 web视图是片段的一部分,在Android Studio中的布局如下: 如您所见,它使用的是RelativeLayout。此外,以下是XML:在Android上,如何将子视图集中在其父视图中?,android,android-relativelayout,Android,Android Relativelayout,我花了很长时间在安卓系统的父视图中集中一个子视图(免责声明:我是一个在安卓系统上几乎没有经验的iOS开发人员)。我想要的非常简单:将一个不确定的进度视图(微调器)放在它的web视图父视图的中心 web视图是片段的一部分,在Android Studio中的布局如下: 如您所见,它使用的是RelativeLayout。此外,以下是XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="h
<?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"
android:gravity="center"
tools:context=".WrappedWebViewFragment">
<WebView
android:id="@+id/fragment_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
</WebView>
</RelativeLayout>
据我所知,RelativeLayout用于相对于其父视图定位子视图,所以我不确定这为什么不起作用。下面是我看到的(微调器出现在左上角):
如何使微调器居中?
WebView
扩展AbsoluteLayout
。因此,RelativeLayout.LayoutParams
的addRule
不适用。此外,由于webview是一个绝对的布局,因此向webview添加视图似乎不是一个好主意。
由于AbsoluteLayout不支持重力属性,因此必须直接计算中心的x和y值。最好在布局文件中写入,以便查看webview并在加载webview时取消进度
如果仍然希望将其直接放置在webview中,可以尝试以下方法。(这似乎不是一个好办法)
}为什么不在xml文件中创建progressbar,我认为它很简单,如下所示:
<?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"
tools:context=".WrappedWebViewFragment">
<WebView
android:id="@+id/fragment_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
</RelativeLayout>
而不是给出(100100)
,您需要使用匹配父项来表示高度和宽度。在lp.addRule中什么是lp
(父项中的相对位置)代码>?您可以共享您的xml吗?@Barns已修复。它应该是layoutParams.addRule(…)代码>@GastónSaillén完成。我没有直接将其添加到web视图,而是将其添加到片段的根视图中,这样做很有效!i、 e.ViewGroup ViewGroup=(ViewGroup)getView();添加视图(微调器、布局参数)代码>它也可以是一种方式。干得好但是为什么要以编程方式添加它呢?
public class MyWebView extends WebView {
private boolean isFirstLayout = true;
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Button v = new Button(getContext());
v.setText("test");
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
v.setLayoutParams(lp);
addView(v);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (isFirstLayout) {
View v = getChildAt(0);
AbsoluteLayout.LayoutParams lp = (LayoutParams) v.getLayoutParams();
lp.x = (getWidth() / 2) - (v.getWidth() / 2);
lp.y = (getHeight() / 2) - (v.getHeight() / 2);
v.setLayoutParams(lp);
isFirstLayout = false;
}
}
<?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"
tools:context=".WrappedWebViewFragment">
<WebView
android:id="@+id/fragment_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
</RelativeLayout>