更改两种布局之间的背景颜色?

时间:2019-08-11 12:52:52

标签: java android android-studio

我有activity_main.xml,有两种版式:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/appColor">

<android.support.constraint.ConstraintLayout
    android:id="@+id/loading_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/appColor">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        android:indeterminateTint="@color/white"
        android:indeterminateTintMode="src_in"/>


</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:id="@+id/webview_layout"
    android:visibility="gone"
    android:background="@color/appColor"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/appColor"/>

</android.support.constraint.ConstraintLayout>

首先,我显示loading_layout,当我加载完应用程序后,显示webview_layout

ConstraintLayout loadingLayout;
ConstraintLayout webviewLayout;

WebView webview;

String kAppUrl = "*******";

String kDefaultAppUrl = "******";

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

    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);

    loadingLayout = (ConstraintLayout)findViewById(R.id.loading_layout);
    webviewLayout = (ConstraintLayout)findViewById(R.id.webview_layout);

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

    webview.clearCache(true);
    webview.clearHistory();
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    startLoadingUrlFromServer();
}

private void startLoadingUrlFromServer() {
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, kAppUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(final String response) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            finishLoadindDataFromServerWithData(response);
                        }
                    });
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            openWebSite(null);
                        }
                    });
                }
    });

    queue.add(stringRequest);
}

private void finishLoadindDataFromServerWithData(String response) {
    if (response == null || response.equals("")) {
        openWebSite(null);
        return;
    }

    if (!response.startsWith("http://") && !response.startsWith("https://")) {
        openWebSite(null);
        return;
    }

    openWebSite(response);
}

private void openWebSite(String url) {
    loadingLayout.setVisibility(View.GONE);
    webviewLayout.setVisibility(View.VISIBLE);

    if (url == null || url.equals("")) {
        webview.loadUrl(kDefaultAppUrl);
    } else {
        webview.loadUrl(url);
    }
}

当我将loadingLayout的可见性设置为GONE并将webviewLayout设置为VISIBLE时,应用程序具有白色背景两秒钟的问题,可以改变背景颜色吗?

1 个答案:

答案 0 :(得分:0)

看起来两秒钟内找到的颜色是应用程序主题背景。

转到您的AndroidManifest.xml文件并打开您的Application标签具有的主题(如果在Activity标签中定义了另一个主题,请打开该主题)。

这应该打开您的styles.xml文件。将item的新windowBackground添加到您的AppTheme(假设这是您的主题名称),这会将白色背景颜色更改为定义的背景颜色(在下面的代码中为黑色)。 / p>

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
        <item name="android:windowBackground">@android:color/black</item>
        ... 
        ..
        .
    </style>

</resources>