java - 更改两个布局之间的背景颜色?

标签 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 时,应用程序出现问题有两秒钟的白色背景,知道如何更改此背景颜色吗?

最佳答案

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

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

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

<resources>

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

</resources>

关于java - 更改两个布局之间的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450092/

相关文章:

java - 如何使用 java 验证 Selenium 中的字符串列表是否按字母顺序排列

android - FCM 通知 click_action 应该重定向到打开浏览器

java - 使用 GeoTrust 的证书回复签署 midlet

android - FFmpeg 低修剪精度

android - 如何更改 TextInputLayout 中未聚焦的 edittext 提示文本颜色的颜色?

Android GridLayout - 如何增加列或行之间的间距?

android-studio - 如何在 M1 Mac 上运行 Android Studio 模拟器?

android - Android Studio Gradle依赖性检查

java - 如何使用正则表达式来检查字符串中的重复字符?

java - 为什么 jmap -permstat 会报告超过使用的 MaxPermSize 字节数?