Android 将WebView放置在线性布局的中心 我尝试了不同的方法,但是没有把WebVIEW放在我的代码>线性布局< /代码>(或屏幕)的中间。
我只在我的Android 将WebView放置在线性布局的中心 我尝试了不同的方法,但是没有把WebVIEW放在我的代码>线性布局< /代码>(或屏幕)的中间。,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我只在我的WebView中加载了一个元素,那就是YouTube视频iFrame 我的布局如下所示,其中包含webview <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.and
WebView
中加载了一个元素,那就是YouTube视频iFrame
我的布局如下所示,其中包含webview
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#0c0000"
android:id="@+id/webviewlayoutVDO">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/loadinglayout"
android:gravity="center">
<ProgressBar
android:id="@+id/theloadingbar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/webviewlayoutVDOInternal">
<WebView
android:id="@+id/webviewVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/videoDetails"
android:gravity="bottom">
<TextView
android:id="@+id/videoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PLAY"
android:textColor="@color/colorPrimary"
android:gravity="bottom|center" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
改变你的线性布局
android:gravity="center"
到
你也可以试试
android:layout_gravity="center"
android:gravity
处理子对象的对齐
android:layou-gravity
处理自身的对齐
中心垂直:将对象放置在其容器的垂直中心,不改变其大小
使用android:layout\u gravity=“center\u vertical”
i、 e
关于:
仅供参考:
fill\u parent
已被弃用,因此您应该使用match\u parent
,除非您的目标是API\u 1到7。将布局的重心设置为中心android:gravity
影响布局的子级,而
android:layou重力影响组件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="@+id/webviewlayoutVDOInternal">
<WebView
android:id="@+id/webviewVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
/>
</LinearLayout>
您必须匹配webview的宽度和高度,如
<WebView android:id="@+id/webviewVideo" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" />
这是一个很好的答案,尽管我尝试了你的两种解决方案,但都不起作用,我尝试了android:gravity=“center\u horizontal”
和android:layout\u gravity=“center”
——Sushingover的解决方案很有效,它是android:layou\u gravity=“center\u vertical”
简单地使用android:layout\u gravity=“中心垂直”
工作正常,感谢您的帮助
<WebView
android:layout_gravity="center_vertical"
android:id="@+id/webviewVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="@+id/webviewlayoutVDOInternal">
<WebView
android:id="@+id/webviewVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
/>
</LinearLayout>
<WebView android:id="@+id/webviewVideo" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" />