【Android】正确使用资源res文件

简介: 首先有的UI改颜色,没用,发现无法更改按钮背景颜色。我的AS下载的是最新版本,Button按钮的背景颜色一直都是亮紫色,无法更改。为什么呢?首先在你的清单文件中看你应用的是哪个主题。

观看此文注意


首先有的UI改颜色,没用,发现无法更改按钮背景颜色。


我的AS下载的是最新版本,Button按钮的背景颜色一直都是亮紫色,无法更改。


为什么呢?


首先在你的清单文件中看你应用的是哪个主题。


52c4e0ea0bffa11bd13452f9ebe71aad.png


我现在用的是这个


f1fae727a497265e5202d25a263c5a09.png


可能你的主题用的是上面的默认的原因。


方法一:


将app/res/themes目录下默认的themes代码加上.Bridge即可。


修改前

<style name="Theme.IntelligentWatch" parent="Theme.MaterialComponents.DayNight.DarkActionBar">


修改后

<style name="Theme.IntelligentWatch" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">


方法二:


自己定义一个主题,就是类似(我用的),但是我这个是没有ActionBar标题栏的,你需要自己再去定义一个标题栏,然后去清单文件中使用自己定义的主题。


<style name="Theme.IntelligentWatch.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>


创建res文件


首先我们创建一个res文件。


右键res文件夹


98fe4e0f8239d6f4ec361602a06516ce.png



(一)Resource type:Values


都是放在res\values文件夹下。


ee2ffc218aed67b94ad9cdc7a2346139.png


其Root element(父节点):只有resources。


32666e226ea76757a6b36186a20550c8.png



1.子元素@String


5adfa7d498d6fdd6fbe097b24202f687.png


Alt+回车可以自动生成。


<resources>
    <string name="title">登录</string>
    <string-array name="ddd">
        <item>"dddd"</item>
        <item>"2222"</item>
    </string-array>
</resources>


双引号可加可不加。


打印出来默认去除,如果想要双引号,则加上转义字符

<string-array name="ddd">
    <item>\"dddd\"</item>
    <item>\"2222\"</item>
</string-array>


怎么使用呢?


在布局文件里:@String


在Java文件里:getResources().getStringArray(...)



2.子元素@Color


一般是在XML布局文件中使用。

组成部分:# +透明度++绿+蓝(ARGB)

每个部分的值:0-256


<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="ivory">#EEEEEE</color>象牙白
    <color name="blue">#3161EF</color>蓝色
    <color name="gray">#B3B1B1</color>灰色
</resources>


3.子元素@dimen


尺寸。比如字体大小等等。

<resources>
    <dimen name="fab_margin">16dp</dimen>
</resources>


(二)Resource type:Drawable


放在res\drawable文件夹下。


39610444ddbae65c39de54a95054e77c.png


其Root element(父节点,这里叫做根元素):有很多,这里主要来讲解其中常用的几个。


8469da2901709a8a5adf7d1ef3c262dc.png


1.根元素@shape


形状。在UI控件的background中设置。


比如我们想要为按钮创建一个背景。


android:shape="rectangle"

ring:圆环 oval:椭圆 rectangle:长方形 line:线


1)子元素@solid

中间内容填充的颜色。


2)子元素@corners

拐角的弯曲的弧度


3)子元素@size

大小。


4)子元素@stroke

描边,一般给EditText输入框给一个背景的时候会用到,这个中间不会填充。

5)子元素@gradient

线性渐变


6)子元素@padding

间距。


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white"/>
    <corners android:radius="100dp"/>
</shape>


<Button
    android:id="@+id/button_first"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="@string/title"
    android:textSize="20dp"
    android:textColor="@color/blue"
    android:background="@drawable/loading_btn"
    android:layout_margin="28dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/textview_first" />


d3793889af4c9d6e7826b1e3d7b7cd19.png


一般设置在XML布局文件的UI控件的background里面,用@drawable/xxx就可以了。



2.根元素@selector


1)子元素@item


选择器。在UI控件的background中设置。

比如我们想要点击按钮的时候是蓝色,不点击的时候是黑色。

不写 android:state_pressed="false"也可以,因为默认是false。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/blue"/>
    <item android:state_pressed="false" android:drawable="@color/black"/>
</selector>


3.根元素@animation-list


帧动画。


1.子元素@item


drawable:显示哪张图片。

duration:显示此图片的时间,以ms为单位。、

设置在background上。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/image01" android:duration="500"/>
    <item android:drawable="@drawable/image02" android:duration="500"/>
    <item android:drawable="@drawable/image03" android:duration="500"/>
</animation-list>


注意:动画需要启动,不然只会显示第一张图片。


    Button button = (Button) findViewById(R.id.bt_001);
    //把Drawable设置为button的背景,也可以设置为根布局的背景
    button.setBackgroundResource(R.drawable.frame_animation);
    //拿到这个我们定义的Drawable,实际也就是AnimationDrawable
    AnimationDrawable anim = (AnimationDrawable) button.getBackground();
    //开启动画
    anim.start();


AnimationDrawable是一个Drawable的子类,所以我们定义的xml文件也是防在res/rawable目录下的

anim.close();//关闭动画


(三)Resources type(文件夹):anim


New Animation Resources File 补间动画。


671128d88cb05870ea046615457be30e.png


1.根元素@set


1)子元素@alpha

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:toAlpha="0.0" />


从不透明变成透明,花费2秒钟。

interpolator表示动画插值器, 可以控制动画的变化速率, 比如前200ms很慢,中间600ms很快,最后200ms又很慢。


2)子元素@rotate

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromDegree="0"
      android:toDegree="360"
      android:pivotX = "50%"
      android:pivotY="50%"
      android:duration = "3000"/>


从0度转360度,转一圈。


轴点是中心,花费3秒。


3)子元素@scale

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0"/>


0.0表示没有显示,1.0表示原始大小。

从0变到1,就是没显示到显示。

以左上角为原点进行移动,50%就是中心点位置。

从中间从远到近显示出来,花费一秒。


4)子元素@translate

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="100"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="0"/>


fromXDelta:X方向的初始值是多少

fromYDelta:Y方向的初始值是多少

toXDelta:X方向的目标值是多少

pivot 属性主要在translate 和 scale 动画中,这两种动画都牵扯到view 的“物理位置“发生变化,所以需要一个参考点。 而pivotX和pivotY就共同决定了这个点;它的值可以是float或者是百分比数值。

以 pivotX 为例:

10:距离动画所在view自身左边缘10像素

10% :距离动画所在view自身左边缘 的距离是整个view宽度的10%

10%p:距离动画所在view父控件左边缘的距离是整个view宽度的10%



属性动画就不需要设置res文件了,



(四)特殊Resources type:Values


Style与Themes


根元素都是resources。

其实你可以发现这两个的子元素都是style,而且效果也差不多。

那这两个有什么区别呢


1.style


针对某个控件,你可以为你的按钮,文字定制一个样式,属于比较小的点。


7c017ddcbc19ae058578f08642d3406d.png


注意要加android:,不然不会生效,因为也不会报错。


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyBtnStyle" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <item name="android:layout_height">50dp</item>
        <item name="android:textSize">25dp</item>
        <item name="android:textColor">@color/blue</item>
        <item name="android:background">@drawable/loading_btn</item>
    </style>
</resources>


在XML布局中使用的时候只需要在UI控件属性中加入下面这句话即可。


style="@style/MyBtnStyle"


style优先级<布局文件属性优先级

也就是说你在XML布局文件中再次写了字体颜色,会覆盖掉style里面的字体颜色。



2.themes


针对整个应用,app是一个怎么样的样式。


在最开始的观看此文有介绍。

目录
相关文章
|
6天前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
2月前
|
Java Android开发 C++
Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手
本文提供了一个Android Studio中JNI使用的模板,包括创建C/C++源文件、编辑CMakeLists.txt、编写JNI接口代码、配置build.gradle以及编译生成.so库的详细步骤,以帮助开发者快速上手Android平台的JNI开发和编译过程。
167 1
|
15天前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
50 0
|
2月前
|
开发工具 git 索引
repo sync 更新源码 android-12.0.0_r34, fatal: 不能重置索引文件至版本 ‘v2.27^0‘。
本文描述了在更新AOSP 12源码时遇到的repo同步错误,并提供了通过手动git pull更新repo工具来解决这一问题的方法。
96 1
|
2月前
|
存储 监控 数据库
Android经典实战之OkDownload的文件分段下载及合成原理
本文介绍了 OkDownload,一个高效的 Android 下载引擎,支持多线程下载、断点续传等功能。文章详细描述了文件分段下载及合成原理,包括任务创建、断点续传、并行下载等步骤,并展示了如何通过多种机制保证下载的稳定性和完整性。
71 0
|
2月前
|
监控 Java 开发工具
### 绝招揭秘!Android平台GB28181设备接入端如何实现资源占用和性能消耗的极限瘦身?
【8月更文挑战第14天】本文介绍在Android平台优化GB28181标准下设备接入的性能方法,涵盖环境搭建、SDK集成与初始化。重点讲解内存管理技巧如软引用、按需加载资源,以及通过硬件加速解码视频数据和图像缩放来减轻CPU与GPU负担。同时采用线程池异步处理视频流,确保UI流畅性。这些策略有助于提高应用效率和用户体验。
39 0
|
4月前
|
Java 开发工具 Android开发
详细解读Android开发DNK开发将.c文件打包成os
详细解读Android开发DNK开发将.c文件打包成os
29 0
|
4月前
|
Android开发
Android Gradle开发—脚本实现自动打包后复制一份APK文件,并修改APK名称,到指定目录作备份
Android Gradle开发—脚本实现自动打包后复制一份APK文件,并修改APK名称,到指定目录作备份
214 0
|
4月前
|
缓存 大数据 Android开发
Android 巧用putBinder方法传递大文件
Android 巧用putBinder方法传递大文件
108 0
|
4月前
|
开发工具 Android开发
Android 代码自定义drawble文件实现View圆角背景
Android 代码自定义drawble文件实现View圆角背景
153 0