java - 更改两个布局之间的背景颜色?
问题描述
我有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
toGONE
和webviewLayout
toVISIBLE
应用程序的 Visibility 的问题有两秒钟的白色背景,知道如何更改此背景颜色吗?
解决方案
看起来您在两秒钟内找到的颜色是应用程序主题背景。
转到您的AndroidManifest.xml
文件并打开您的Application
标签所具有的主题(如果在活动标签中定义了另一个主题,请打开它)。
这应该会打开您的styles.xml
文件。item
为windowBackground
您的(假设这是您的主题名称)添加新的AppTheme
,这会将白色背景颜色更改为定义的颜色(在下面的代码中,它的黑色)。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
<item name="android:windowBackground">@android:color/black</item>
...
..
.
</style>
</resources>
推荐阅读
- php - 如何通过 5 个字符的 HTML 形式限制我的用户输入,以大写 S 开头,后跟四个数字?
- c++ - GL_TRANSFORM_FEEDBACK_BUFFER -- 获取几何着色器发出的三角形
- r - RVest:从超市网页中提取类内的标签内容
- android - Nearby Connections 中是否有任何安全措施?
- php - Composer - 无法安装 github 包
- javascript - 带有 TOTP 密码的 POST 请求:对预检请求的响应未通过访问控制检查
- python - 从 Pandas 中的多行正则表达式创建新列
- react-native - React Native:如何使用自定义内容制作推送通知?
- java - 如何在 SpringBoot 中获取 Rsocket 连接的远程 IP 地址
- c# - 条件断点错误:尝试将 lambda 传递给 VS 2019 中的委托 C#,替代