首页 > 解决方案 > 更改两个布局之间的背景颜色?

问题描述

我有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);
    }
}

当我设置loadingLayouttoGONEwebviewLayouttoVISIBLE应用程序的 Visibility 的问题有两秒钟的白色背景,知道如何更改此背景颜色吗?

标签: javaandroidandroid-studio

解决方案


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

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

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

<resources>

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

</resources>

推荐阅读