Android开发-软件版本升级与黑暗模式的适配【Android 10】,全套学习

android:layout_width=“190dp”

android:layout_height=“40dp”

android:layout_marginStart=“10dp”

android:layout_marginTop=“5dp”

android:gravity=“start”

android:text="@string/tv_text"

android:textColor="@color/colorUnitText"

android:textSize=“12sp”

app:layout_constraintStart_toEndOf="@+id/iv_icon"

app:layout_constraintTop_toBottomOf="@+id/tv_title" />

<TextView

android:id="@+id/tv_time"

android:layout_width=“100dp”

android:layout_height=“15dp”

android:layout_marginEnd=“10dp”

android:layout_marginBottom=“20dp”

android:gravity=“end|center_vertical”

android:text="@string/tv_time"

android:textSize=“12sp”

android:textStyle=“italic”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent” />

<ImageView

android:id="@+id/iv_remove"

android:layout_width=“35dp”

android:layout_height=“35dp”

android:layout_marginEnd=“30dp”

android:layout_marginBottom=“10dp”

android:contentDescription="@string/iv_remove"

app:layout_constraintBottom_toTopOf="@+id/tv_time"

app:layout_constraintEnd_toEndOf=“parent”

app:srcCompat="@drawable/icon_delete" />

<ImageView

android:id="@+id/iv_icon"

android:layout_width=“60dp”

android:layout_height=“60dp”

android:layout_marginStart=“15dp”

android:layout_marginTop=“10dp”

android:contentDescription="@string/iv_remove"

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:srcCompat="@drawable/icon_notepad" />

<TextView

android:id="@+id/tv_id"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textColor="#00FF0000"

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_notepad.xml
<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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”

android:fitsSystemWindows="tr

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ue"

tools:context=".ActivityNotepad">

<ScrollView

android:id="@+id/sv"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background="#FFFFFF"

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“0.0”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintVertical_bias=“0.0”>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background="@color/colorNotepadBackground"

android:orientation=“vertical”>

<EditText

android:id="@+id/tv_title"

android:layout_width=“match_parent”

android:layout_height=“60dp”

android:layout_margin=“10dp”

android:autofillHints=""

android:ems=“10”

android:fontFamily=“sans-serif-black”

android:gravity=“start”

android:hint="@string/et_title_hint"

android:inputType=“textPersonName”

android:textColor="@color/colorNotepadTitle"

android:textSize=“30sp” />

<com.example.notepad.MultilineTextEditWithUnderLine

android:id="@+id/et_text"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“10dp”

android:background="@null"

android:gravity=“start”

android:hint="@string/et_text_hint"

android:letterSpacing=“0.03”

android:lineSpacingMultiplier=“1.5”

android:maxLines=“30”

android:minLines=“20”

android:textColor="@color/colorNotepadText"

android:textSize=“18sp” />

</androidx.constraintlayout.widget.ConstraintLayout>

更改values/color.xml(白天模式)
<?xml version="1.0" encoding="utf-8"?>

#6200EE

#3700B3

#03DAC5

#CCCCFF

#000000

#111111

#FFFFFF

#000000

#111111

更改values-night/color.xml(黑夜模式)
<?xml version="1.0" encoding="utf-8"?>

#6200EE

#3700B3

#03DAC5

#555555

#FFFFFF

#EEEEEE

#777777

#FFFFFF

#EEEEEE

可以看出,AS默认的白天模式和在黑夜模式的手机上表现出了不一样的效果,这是系统自己适配的,图标的适配也一样,使用动态编码替换黑白两种图标,就能更加适配主题。

最后,在Java代码里来获取夜间模式的开启状态,便于我们动态做出更多选择。

//配置当前模式

protected void configTheme()

{

//选择当前的主题模式

int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

switch (currentNightMode) {

case Configuration.UI_MODE_NIGHT_NO:

// 夜间模式未开启

break;

case Configuration.UI_MODE_NIGHT_YES:

// 夜间模式已开启

break;

}

}

至此,简单的迁移和适配已经结束,更多API可以参阅Google官方文档。
原文链接:https://blog.csdn.net/zwq939681378/article/details/111469662
作者:朱蔚钦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值