Загрузка WebView предотвращает загрузку других видов в макете - PullRequest
0 голосов
/ 24 мая 2018

Я пытаюсь создать макет, в котором есть WebView и другие представления, такие как TextView.

В приведенном ниже коде я ожидаю, что WebView будет отображаться на первых 300 dp по высоте и TextView.в нижней части экрана с текстом «Тестовый текст»

Прямо сейчас, когда запускается код для загрузки URL-адреса в WebView (с фрагментом кода ниже), WebView успешно загружаетurl, однако , другие представления игнорируются и не загружаются.

Может кто-нибудь помочь мне загрузить WebView и другие представления на одном экране?

Я пытался переключаться между:

WebView webView = findViewById(R.id.temp_web_view);

и

WebView webView = findViewById(this);

Также,

setContentView(webView);

и

setContentView(R.layout.activity_profiling_result);

но пока не повезло ..

SomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profiling_result);

    WebView webView = findViewById(R.id.temp_web_view);

    // must call removeView() on the child's parent first.
    if (webView.getParent() != null) {
        ((ViewGroup)webView.getParent()).removeView(webView);
    }

    setContentView(webView);
    webView.getSettings().setJavaScriptEnabled(true);

    String WEB_URL = "https://www.google.ca/";
    webView.loadUrl(WEB_URL);
}

activity_profiling_result.xml

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

    <WebView
        android:id="@+id/temp_web_view"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <TextView
        android:id="@+id/temp_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Test Text" />

</RelativeLayout>

Ответы [ 2 ]

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

удалить эти коды, это будет работать так, как вы ожидаете

if (webView.getParent ()! = Null) {((ViewGroup) webView.getParent ()). RemoveView (webView);} setContentView (webView);

Ваш окончательный код должен быть:

@ Переопределить защищенный void onCreate (Bundle saveInstanceState) {super.onCreate (saveInstanceState);setContentView (R.layout.activity_profiling_result);WebView webView = findViewById (R.id.temp_web_view);webView.getSettings () setJavaScriptEnabled (истина).Строка WEB_URL = "https://www.google.ca/"; webView.loadUrl (WEB_URL);}

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

Удалите это

setContentView(webView);
// must call removeView() on the child's parent first.
if (webView.getParent() != null) {
    // this will remove the webView from the customise layout
    ((ViewGroup)webView.getParent()).removeView(webView);
}

, поскольку оно заменит весь макет экземпляром webView.

setContentView (R.layout.activity_profiling_result);

Присоединение и отображение настраиваемого макета (веб-просмотр или просмотр текста и т. Д.)

setContentView (веб-просмотр);

Присоединение и отображение экземпляра веб-просмотра

setContentView

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.Это может быть сложная иерархия представлений.При вызове этого метода параметры макета указанного представления игнорируются.Both the width and the height of the view are set by default to MATCH_PARENT. Чтобы использовать собственные параметры макета, вместо этого вызовите setContentView (android.view.View, android.view.ViewGroup.LayoutParams).


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profiling_result);

    WebView webView = findViewById(R.id.temp_web_view);

    // must call removeView() on the child's parent first.
    //if (webView.getParent() != null) {
        //((ViewGroup)webView.getParent()).removeView(webView);
    //}

    //setContentView(webView);
    webView.getSettings().setJavaScriptEnabled(true);

    String WEB_URL = "https://www.google.ca/";
    webView.loadUrl(WEB_URL);
}
...