我正在开发一个具有多种风格的 android 项目。
这很好用,我可以自定义应用程序的元素,例如颜色和字符串资源。
我想让它使一些风格基于 AppCompat 浅色主题,有些风格基于 AppCompat 深色主题。
我知道我可以通过<items>在我的风格和每种风格中重复所有内容来实现这一点,将主题设置为指向每个应用程序的自定义清单中的每个主题,但这似乎有点过分。
有没有一些简单的方法来设置这样的主题
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/primary_colour_tone1</item>
...lots more settings
</style>
Run Code Online (Sandbox Code Playgroud)
但是不是直接指向 Theme.AppCompat.NoActionBar 指向在每个 gradle 构建中可以设置不同的引用?
所以像这样:
<style name="AppTheme" parent="**Theme reference here**">
<item name="colorPrimary">@color/primary_colour_tone1</item>
...lots more settings
</style>
Run Code Online (Sandbox Code Playgroud)
我试过使用 @string/theme_name 之类的东西,但这不起作用,有没有简单的方法来做到这一点?
我的自定义应用程序主题是CustomTheme,它的父主题是Theme.AppCompat.Light.NoActionBar. 我没有textColorPrimaryInverse在我的自定义主题中定义项目。在colors.xml我定义哪些我想成为等于一个颜色textColorPrimaryInverse的属性Theme.AppCompat.Light.NoActionBar。所以我这样引用它:
<color name="enabled_blue_button_text">?android:attr/textColorPrimaryInverse</color>
Run Code Online (Sandbox Code Playgroud)
这给了我一个运行时异常“找不到与给定名称匹配的资源”。如果我引用?android:attr/textColorPrimaryInverseinstyles.xml而不是colors.xml它工作正常。
textColorPrimaryInverse毕竟,如果我在自定义主题中定义..
<item name="android:textColorPrimaryInverse">@android:color/white</item>
Run Code Online (Sandbox Code Playgroud)
...引用colors.xml作品。
问题 1. 为什么我不能从 colors.xml 中引用 android 主题属性
但我不想在我的自定义主题中定义这个属性。至少我准备将它定义为等于父属性。
问题2.如果没有在自定义主题中重新定义这个属性就没有办法,我可以这样定义吗:
<item name="android:textColorPrimaryInverse">ref to parent attribute</item>
?
我想使用 style.xml 文件更改 MaterialCardView 的背景颜色。它真的有助于在我的 Android 应用程序中实现“暗模式”。
我尝试过动态执行,但更喜欢使用主题来完成。
我正在使用首选项库(androidx.preference:preference)以及材质组件主题(Theme.MaterialComponents.Light.DarkActionBar)。我正在寻找一种正确的方法来设置类别文本的样式(字体、大小、颜色)(不更改整个布局)以及首选项文本本身。官方首选项库示例展示了一种在字符串资源内使用 HTML 标签显示样式首选项的方法,但我正在寻找一种通过主题更改它(和标题)的方法:
android android-theme android-styles preferencefragment material-components-android
我有一个包含多个皮肤的应用程序。在我定义的一个皮肤中android:colorBackground,然后我想在 XML 布局中将此颜色设置为 ViewGroup 背景颜色。我该怎么做?是否可以?或者它只是系统使用的某些属性?
<style name="SkinDefault" parent="@style/_SkinXYZ">
<item name="android:colorBackground">@color/skin_default_color_background</item>
<item name="colorOnBackground">@color/skin_default_color_on_background</item>
</style>
Run Code Online (Sandbox Code Playgroud) android android-theme android-attributes material-components-android
我正在为 RecyclerView 项目使用环绕 EditText 的 TextInputLayout 。我正在使用样式“Theme.AppCompat.Light.NoActionBar”。但我仍然收到错误“由:java.lang.IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.AppCompat(或后代)”。 但是,当我从 xml 中删除 TextInputLayout 时,应用程序运行良好。
这是我的 RecyclerView 项目(item_content_settings):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="20">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="18"
android:padding="16dp">
<EditText
android:id="@+id/content_summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="First Name"
android:inputType="text"
android:text="First Name And Last Name" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="@+id/pencil"
android:layout_width="0dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="2"
android:src="@drawable/pencil_edit" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的风格:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Base" …Run Code Online (Sandbox Code Playgroud) android android-layout android-theme android-textinputlayout material-components-android
我使用的是谷歌库调用播放服务,插件,以包括开源通知我的应用程序。
我可以毫无问题地实施它,但我不知道如何设计显示许可证的活动。
在我的代码中,我通过调用开始活动 startActivity(Intent(this, OssLicensesMenuActivity::class.java))
由于OssLicensesMenuActivity是一个 Activity 类,我设法设置如下样式:
styles.xml:
<style name="LicensesTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryDark</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml:
<activity
android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
android:theme="@style/LicensesTheme" />
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力,但此活动仅显示我正在使用的所有库。还有第二个活动显示许可证,这是我无法弄清楚如何设计样式的活动。我面临的问题是代码OssLicensesMenuActivity被混淆了,我找不到我想要设置样式的活动类的名称和包。
有谁知道我该如何解决这个问题?
为了更容易检查,混淆的代码OssLicensesMenuActivity如下:
package com.google.android.gms.oss.licenses;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager.LoaderCallbacks;
import androidx.loader.content.Loader;
import …Run Code Online (Sandbox Code Playgroud) android android-theme google-play-services android-studio android-styles
我正在我的应用程序中开发 Day/Night 功能,所以我阅读了这些文档并开始开发它。
使用 deligate 方法在 Day 或 Night 中使用默认值可以正常工作AppCompatDelegate.setDefaultNightMode(*)。
为了自定义夜间主题颜色,我创建了values-night文件夹,并在里面创建了如下所示的colours.xml文件。
res -> values -> colors.xml
res -> values-night -> colors.xml
Run Code Online (Sandbox Code Playgroud)
在我放置该颜色但未应用于 Night 主题之后!很奇怪为什么价值夜间颜色并不总是应用其显示的默认夜间颜色?我研究了一些,但找不到解决方案。
注意:重新创建活动解决了我的问题,但我不想重新创建
这是我的 build.gradle 文件
提前谢谢。
您将如何通过按下应用程序内的按钮在主题的调色板之间动态切换
这是我到目前为止所做的,但仅在我将 Android 主题切换到暗模式或亮模式时才有效
应用主题.Kt
@Model
object ThemeState {
var isLight: Boolean = true
}
@Composable
fun MyAppTheme(
children: @Composable() () -> Unit
) {
MaterialTheme(colors = if (ThemeState.isLight) themeColorsLight else themColorDark) {
children()
}
}
Run Code Online (Sandbox Code Playgroud)
主活动.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyAppTheme(children = {
Surface {
Greetings(name = "Android")
}
})
}
}
}
@Composable
fun Greetings(name: String) {
Column(modifier = Modifier.fillMaxHeight()) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = "Hello …Run Code Online (Sandbox Code Playgroud) 我想移动所有与主题相关的 xml 代码来编写文件。有一个闪屏 xml 代码,是否可以让它在撰写风格中使用?
<!-- Splash screen theme. -->
<style name="splashScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
@Composable
fun splashScreenTheme() {
}
Run Code Online (Sandbox Code Playgroud)