基于 LibGdx 的 Android 应用程序中指定区域的 WebView

WebView for secified Area in LibGdx based Android app

我已经为加密货币构建了一个应用程序,以维护投资组合。我打算在同一个应用程序中显示最新的新闻项目。

整个项目是在 LibGdx 中完成的 Android 应用程序。

问题

  1. 如何获得区域"Part-B"的网络视图?

  2. LigGdx 有类似 WebView 的东西吗?

  3. LibGdx 是否有任何扩展来处理 WebView?

截图

从你最后两个问题开始;不。 LibGDX 目前没有扩展,也没有对网络视图或类似内容的本地支持。但是,您可以创建自定义布局。 LibGDX 有一个 initializeForView 方法,你可以用它来抓取视图本身。这是在添加到布局本身的片段中膨胀的。 WebView 就是另一个视图。

首先,启动器:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class AndroidLauncher extends FragmentActivity implements AndroidFragmentApplication.Callbacks {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.gdxwebview);

        // Create the fragment
        GDXWithWebview fragment = new GDXWithWebview();

        getSupportFragmentManager().beginTransaction().
                add(R.id.fragmentRoot, fragment).
                commit();
    }

    @Override
    public void exit() {}


}

由于涉及到碎片,启动器与生成的启动器不同

对于片段:

使用片段的原因是将 LibGDX 膨胀到片段中,最终成为 activity 的子单元。如果您在 activity 中设置内容视图,您也不会在其中获得 WebView。

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class GDXWithWebview extends AndroidFragmentApplication{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // return the GLSurfaceView on which libgdx is drawing game stuff
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
        return initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
    }
}

最后,布局。这是一个基本布局,您可能需要调整权重(/大小)才能使其看起来像您想要的那样

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class AndroidLauncher extends FragmentActivity implements AndroidFragmentApplication.Callbacks {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.gdxwebview);//The layout; comes later

        // Create the fragment
        GDXWithWebview fragment = new GDXWithWebview();

        getSupportFragmentManager().beginTransaction().
                add(R.id.fragmentRoot, fragment).
                commit();//Add the fragment 
    }

    @Override
    public void exit() {}


}

此外,如果您打算使用WebView上网,则需要有上网权限(如果您使用它在本地执行JavaScript或其他网页内容,不上网,您应该不需要它):

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class GDXWithWebview extends AndroidFragmentApplication{
    View root;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
        // return the GLSurfaceView on which libgdx is drawing game stuff
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
        root = initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
        //declaredInTheClassWebView = (WebView) root.findViewById(R.id.webView);//For initialization, make sure you call root.findViewById, not findViewById. You have to specify the view in which to find the ID when dealing with Fragments.
        return root;
    }
}

并且根据 LibGDX documentation on use with Fragments,您还需要 v4 支持库。假设您使用 Gradle 4.1 和 Android Studio:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
    android:id="@+id/fragmentRoot"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    </FrameLayout>

    <WebView android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

</LinearLayout>

如果您使用早期版本的 Gradle 和 Gradle 插件:

<uses-permission android:name="android.permission.INTERNET" />

如果您不使用 Gradle,请为您的构建系统找到合适的调用。如果您没有构建系统,请手动获取 jar 并将其添加到类路径中。