colorprimaryandroid
Ⅰ Android QMUI实战:实现APP换肤功能,并自动适配手机深色模式
要在Android应用中使用QMUI库实现APP换肤功能,并自动适配手机深色模式,可以按照以下步骤进行:
基础设置与集成:
- 创建一个新的Android项目。
- 添加QMUI库的依赖。
- 定义核心属性,如colorPrimary,colorBg13等,为不同的皮肤主题和默认主题创建相应的styles。
- 在colors.xml文件中配置颜色值。
样式继承:
- 利用QMUI的样式继承功能,为不同的皮肤主题创建基于基础主题的扩展。
- 创建一个QDSkinManager管理类,负责加载和切换皮肤。
皮肤持久化与深色模式感知:
- 使用QDPreferenceManager保存皮肤索引,以便下次启动时恢复。
- 确保QDSkinManager能够检测设备的深色模式,并在需要时切换到预设的深色皮肤。
应用启动与自定义Application:
- 在自定义的Application类中初始化QDSkinManager,并适应深色模式。
- 在AndroidManifest.xml中指定应用的启动类。
主活动布局与皮肤应用:
- 在主活动的布局文件中使用QMUI的换肤属性来控制控件的外观。
- 在代码中注册QMUISkinManager。
QMUI的换肤功能扩展:
- 利用QMUI提供的辅助工具来扩展和优化换肤功能。
- 这些工具可以帮助您更灵活地配置和管理皮肤的切换和应用。
重点: QDSkinManager 是实现换肤功能的核心组件,负责加载和切换皮肤。 QDPreferenceManager 用于保存和恢复皮肤设置,确保用户选择的皮肤在下次启动时仍然有效。 自定义Application 类是初始化和管理换肤功能的关键位置。 布局文件中的QMUI换肤属性 是实现控件皮肤切换的直接方式。
通过以上步骤,您可以在Android应用中实现强大的换肤功能和深色模式适配能力,为用户提供个性化的视觉体验。
Ⅱ Android的supportV7中默认按钮的颜色设置
我们知道,在styles.xml文件里面可以设置主题,在主题中设置的一些颜色,将会应用到默认的AppCompat控件上,从而很简单的就可以保持整个APP在UI上的一致性。下面是一个例子:
至于各种控件是如何应用这些颜色设置的,则需要经过更多的尝试了。
比如Activity导航栏默认的图标颜色是colorControlNormal,导航栏的底色是colorPrimary,沉浸式状态栏默认的颜色是colorPrimaryDark;
比如FAB的默认颜色是colorAccent;
比如AppCompatCheckBox默认的选中状态的颜色是colorAccent,而默认的未选择状态的颜色的colorControlNormal;
比如AppCompatSpinner的下拉图标的默认颜色也是colorControlNormal。
......
其实涉及到的主要的就是下面这几个参数:
那么问题来了,如果你使用蓝色的沉浸式状态栏,导航栏上的图标则使用白色,那在这个Activity中使用AppCompatCheckBox的时候,未选择状态就也是白色的,此时如果在白色的背景色下,用户就看不出这是个AppCompatCheckBox了。这时候怎么办?如下图(图中使用的是AppCompatSpinner):
其实很简单,在这个AppCompatCheckBox上使用app:theme="@style/MyCheckBox",然后在styles.xml中添加新的
但是需要注意的是,这样可能引起控件其他默认属性的变化,比如CheckBox的textSize会变成1(不使用app:theme的时候和APP的默认字体大小一样)。
android:theme和app:popupTheme的作用,以及在android 3.0以下不起作用问题的解决
Ⅲ android 沉浸式状态栏和透明状态栏的区别
注意!两种方法的区别:
第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:
第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:
第一种使用方法:
第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>123
values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>1234
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>123456
第二、在清单文件中配置需要沉浸式状态栏的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />12
第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:
1.当背景为图片时,布局可以这么写:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/imgs_bj"
android:fitsSystemWindows="true">
</RelativeLayout>12345678
效果:
2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色
<?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="match_parent"
android:background="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--标题布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是标题"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //内容区域背景设置成白色
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="显示信息"
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
Ⅳ Android中attr/colorOnPrimary的使用
?attr/colorOnPrimary是一个预定义的颜色属性,用在Android开发中。它在主题文件中预先设置了一个颜色值,方便开发者在应用中引用。
在Theme.Sunflower主题文件中,?attr/colorOnPrimary对应的颜色值被定义为@color/sunflower_yellow_500。这个颜色值可以被应用于需要颜色的任何地方。例如,当我们将android:fillColor="?attr/colorOnPrimary"设置在SVG图片的填充属性上时,图片的填充颜色会根据应用的主色调自动调整。
在Java代码中,我们同样可以通过引用?attr/colorOnPrimary来获取对应的颜色值,从而应用于需要颜色的控件或者布局中。这种方式不仅简化了颜色管理,也使得应用的视觉风格更加统一。
Ⅳ ConstraintLayout中属性layout_constraintDimensionRatio的使用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h,2:1"
android:background="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout>
当app:layout_constraintDimensionRatio=“h,2:1” 中写了h的情况下代表高宽比 2 : 1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="w,1:2"
android:background="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout>
当app:layout_constraintDimensionRatio=“w,1:2” 中写了w的情况下代表宽高比1 : 2