当前位置:   article > 正文

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

res文件

观看此文注意

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

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

为什么呢?

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

我现在用的是这个

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

方法一:

将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标题栏的,你需要自己再去定义一个标题栏,然后去清单文件中使用自己定义的主题。

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

创建res文件

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

右键res文件夹


(一)Resource type:Values

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

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


  1. 子元素@String

Alt+回车可以自动生成。

  1. <resources>
  2.     <string name="title">登录</string>
  3.    
  4.     <string-array name="ddd">
  5.     <item>"dddd"</item>
  6.     <item>"2222"</item>
  7.     </string-array>
  8. </resources>

双引号可加可不加。

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

  1. <string-array name="ddd">
  2. <item>\"dddd\"</item>
  3. <item>\"2222\"</item>
  4. </string-array>

怎么使用呢?

在布局文件里:@String

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


2.子元素@Color

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

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

每个部分的值:0-256

  1. <resources>
  2. <color name="purple_200">#FFBB86FC</color>
  3. <color name="purple_500">#FF6200EE</color>
  4. <color name="purple_700">#FF3700B3</color>
  5. <color name="teal_200">#FF03DAC5</color>
  6. <color name="teal_700">#FF018786</color>
  7. <color name="black">#FF000000</color>
  8. <color name="white">#FFFFFFFF</color>
  9. <color name="ivory">#EEEEEE</color>象牙白
  10. <color name="blue">#3161EF</color>蓝色
  11. <color name="gray">#B3B1B1</color>灰色
  12. </resources>

3.子元素@dimen

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

  1. <resources>
  2. <dimen name="fab_margin">16dp</dimen>
  3. </resources>

(二)Resource type:Drawable

放在res\drawable文件夹下。

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

  1. 根元素@shape

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

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

android:shape="rectangle"

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

1)子元素@solid

中间内容填充的颜色。

2)子元素@corners

拐角的弯曲的弧度

3)子元素@size

大小。

4)子元素@stroke

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

5)子元素@gradient

线性渐变

6)子元素@padding

间距。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <solid android:color="@color/white"/>
  4. <corners android:radius="100dp"/>
  5. </shape>
  1. <Button
  2. android:id="@+id/button_first"
  3. android:layout_width="match_parent"
  4. android:layout_height="50dp"
  5. android:text="@string/title"
  6. android:textSize="20dp"
  7. android:textColor="@color/blue"
  8. android:background="@drawable/loading_btn"
  9. android:layout_margin="28dp"
  10. app:layout_constraintBottom_toBottomOf="parent"
  11. app:layout_constraintEnd_toEndOf="parent"
  12. app:layout_constraintStart_toStartOf="parent"
  13. app:layout_constraintTop_toBottomOf="@id/textview_first" />

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


  1. 根元素@selector

1)子元素@item

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

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

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

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

  1. 根元素@animation-list

帧动画。

  1. 子元素@item

drawable:显示哪张图片。

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

设置在background上。

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

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

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

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

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

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

New Animation Resources File 补间动画。

  1. 根元素@set

1)子元素@alpha

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

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

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

2)子元素@rotate

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

从0度转360度,转一圈。

轴点是中心,花费3秒。

3)子元素@scale

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

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

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

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

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

4)子元素@translate

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:duration="1000"
  4. android:fromXDelta="100"
  5. android:fromYDelta="0"
  6. android:toXDelta="0"
  7. 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

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

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

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

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

style="@style/MyBtnStyle"

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

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


2.themes

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

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号