在 Android 中使用 Async 时工具栏不显示

Toolbar doesn't show up when using Async in Android

我有一个带有 webview 的 AppCompatActivity 并从 RAW 文件夹加载 html 到它。我的 XML 中包含工具栏。奇怪的是工具栏没有出现。

要执行以下操作:我正在尝试以下操作:

工具栏XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

XML:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <WebView
        android:id="@+id/loadHTMLView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/progressBarView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true" />

</RelativeLayout>

Java 文件:

@SuppressLint("SimpleDateFormat")
public class AboutUsActivity extends AppCompatActivity
{

    private WebView webView;
    private ProgressBar mIndeterminateProgress;
    private AsyncTask<Void, Void, String> mLicenseLoader;
    private String versionName;
    private String deviceId;
    private Toolbar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.aboutus);

        mActionBar = (Toolbar) findViewById(R.id.toolbar);
        if (mActionBar != null)
        {
            setSupportActionBar(mActionBar);
        }

        getSupportActionBar().setTitle(getResources().getString(R.string.aboutUS));

        mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
        mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onBackPressed();
            }
        });

        try 
        {
            versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            deviceId = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
        }
        catch (NameNotFoundException e) 
        {
            e.printStackTrace();
        }

        mIndeterminateProgress = (ProgressBar)findViewById(R.id.progressBarView);
        webView = (WebView) findViewById(R.id.loadHTMLView);

        loadaboutus();
    }

    private void loadaboutus() 
    {
        mLicenseLoader = new AsyncTask<Void, Void, String>() 
                {

            @Override
            protected String doInBackground(Void... params) 
            {
                InputStream rawResource = getResources().openRawResource(R.raw.about_us);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(rawResource));

                String line;
                StringBuilder sb = new StringBuilder();

                try 
                {
                    while ((line = bufferedReader.readLine()) != null) 
                    {
                        if (line.equalsIgnoreCase("<p>Version:</p>"))
                        {
                            line = "<p>Version: " + versionName + "</p>";
                        }

                        if (line.equalsIgnoreCase("<p>Device Id:</p>"))
                        {
                            line = "<p>Device Id: " + deviceId + "</p>";
                        }

                        sb.append(line);
                        sb.append("\n");
                    }
                    bufferedReader.close();
                } 
                catch (IOException e) 
                {

                }

                return sb.toString();
            }

            @Override
            protected void onPostExecute(String licensesBody) 
            {
                super.onPostExecute(licensesBody);
                if (isCancelled()) return;

                mIndeterminateProgress.setVisibility(View.INVISIBLE);
                webView.setVisibility(View.VISIBLE);
                webView.loadDataWithBaseURL(null, licensesBody, "text/html", "utf-8", null);
                mLicenseLoader = null;
            }

        }.execute();
    }

    @Override
    public void onDestroy() 
    {
        super.onDestroy();
        if (mLicenseLoader != null) 
        {
            mLicenseLoader.cancel(true);
        }
    }
}

然后我意识到如果我评论 loadaboutus();工具栏正确加载。

有什么问题吗?有人可以帮我吗?

谢谢!

您应该在本地创建一个新的 AsyncTask 并指定<输入参数、进度参数、输出结果>。或者使用服务 class 和广播接收器 .

您的工具栏似乎被 WebView 覆盖了。试试这个:

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<WebView
    android:id="@+id/loadHTMLView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar" />

这会将 WebView 置于屏幕 Y 坐标意义上的工具栏下方,而不是 Z 顺序意义上的。

这与AsyncTask无关。

您尚未在相对布局中定义视图元素的位置。使用 layout_below 属性。

使用这样的东西:

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
<WebView
    android:id="@+id/loadHTMLView"
    android: layout_below= "@id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone" />

<ProgressBar
    android:id="@+id/progressBarView"
    android: layout_below= "@id/loadHTMLView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:indeterminate="true" />