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

时间:2015-07-06 01:33:16

标签: android android-toolbar

我有一个带有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();工具栏正确加载。

可能有什么不对?有人可以帮助我吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您应该在本地创建一个新的AsyncTask,指定&lt;输入参数,进展参数,输出结果&gt; 。或者使用Service class和Broadcast receiver。

答案 2 :(得分:0)

您的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顺序感中。

相关问题