Разместите WebView в центре LinearLayout - PullRequest
0 голосов
/ 26 мая 2018

Я пробовал разные методы, но не смог разместить веб-просмотр в середине моего LinearLayout (или экрана)

Я загружаю только один элемент в моем WebView, который является YouTube Video iFrame

My Layout является следующим, который содержит веб-просмотр

<?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>

Результаты следующие

All the blank space

Я хочу, чтобы он был прямо посередине вместо загрузки iframe вверху.

Я попытался добавить css, чтобы центрировать iframe, но я закончилдо создания беспорядка.

Есть ли способ сделать это?

Ниже приведен мой код веб-просмотра

webView.Settings.JavaScriptEnabled = true;
webView.Settings.LoadWithOverviewMode = true;
webView.Settings.UseWideViewPort = false;
webView.SetBackgroundColor(Android.Graphics.Color.Black);
webView.LoadDataWithBaseURL("https://.", IFRAME_CONTENT, "text/html", "UTF-8", null);

Ответы [ 4 ]

0 голосов
/ 27 мая 2018

Вы должны соответствовать ширине и высоте веб-просмотра, как

<WebView android:id="@+id/webviewVideo" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" />
0 голосов
/ 26 мая 2018

center_vertical: Поместить объект в вертикальный центр его контейнера, не меняя его размера.

Использовать android:layout_gravity="center_vertical"

т.е.

<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" />

re: https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams

К вашему сведению: fill_parent устарело, и поэтому вам следует использовать match_parent, если вы не нацеливаетесь на API_1 - 7.

0 голосов
/ 26 мая 2018

Установите гравитацию макета в центр.

android:gravity влияет на дочерние элементы макета, а

android:layout_gravity влияет на компонент

<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>
0 голосов
/ 26 мая 2018

изменить внутри вашего LinearLayout с

android:gravity="center"

на

android:gravity="center_horizontal"

Также вы можете попробовать

android:layout_gravity="center"

android:gravity обрабатывает выравнивание своих дочерних элементов,

android:layout_gravity самостоятельно выполняет выравнивание.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...