android res目录资源使用说明

一、颜色
颜色资源应该位于标签下,路径res/values/colors.xml。
名字可以随意定义value,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<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="testblack">#0000dd</color>
</resources>

在布局xml文件中使用
例如:activity_main.xml

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:text ="test black"
android:textColor="@color/testblack"/>

在activity文件中使用
使用 Resourse.getValues.getColor或R.标签名."name"属性,如:R.color.cname / getColor(R.color.name)

int testblack = getResources().getColor(R.color.testblack);
tv.setTextColor(testblack);
tv.setBackgroundColor(testblack);

二、字串
字串资源应该位于标签下,路径res/values/strings.xml
定义value

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ResrouseTestActivity!</string>
    <string name="app_name">ResrouseTest</string>
    <string name="app_name">testDrawable</string>
	<string name="testTv">测试string引用</string>
</resources>

在布局xml文件中使用
例如:actvity_main.xml

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:text ="@string/testTv" />

在actvity文件中使用
使用 Resourse.getValues.getString或R.标签名.name属性,如:R.String.sname。 (@string/name 、 getResources().getString(R.string.name)

String testString = getResources().getString(R.string.testTv);
tv.setText(testString);

三、图片
图片资源一般使用png格式,Android系统也支持jpg、gif、9png等格式,可以使用Movie来播放gif格式的图片,路径res/drawable。图片也可以是xml等配置文件(一般用于自定义组件)。
在res/drawable 中加入图片资源
在这里插入图片描述
在activity_main.xml文件使用

<Button
android:id="@+id/testBt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
android:text="testBt" />

在代码中使用 getDrawable或R.drawable.图片名

Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
testBt.setBackground(drawable);

四、图片的颜色
位于res/values/my_drawable.xml名字随意

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="solid_red">#FF0000</drawable>
</resources>

定义用于填充一个组件的颜色值,即给view设置背景色。用法和drawable下的图片一样。

五、单位资源
定义用于填充一个组件的颜色值,即给view设置背景色。用法和drawable下的图片一样。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dimen_name">2px</dimen>
<dimen name="dimen_px">5px</dimen>
<dimen name="dimen_pt">3pt</dimen>
<dimen name="dimen_dp">3dp</dimen>
</resources>

在activity_main.xml布局文件中使用

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:textSize="@dimen/dimen_sp"
android:text ="@string/testTv" />

在activity 代码中引用

float testSize = getResources().getDimension(R.dimen.dimen_sp);
tv.setTextSize(testSize);

六、菜单
菜单即可以从代码中实现也可以在资源文件中配置

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item  
        android:id="@+id/previous"  
        android:title="@string/previous"  
        android:enabled="false"  android:icon="@android:drawable/ic_media_previous"/>
        <item  
        android:id="@+id/play_pause"  
        android:title="@string/play"  
        android:icon="@android:drawable/ic_media_play"/>
    <item  
        android:id="@+id/next"  
        android:title="@string/next"  
        android:icon="@android:drawable/ic_menu_next"/>
</menu>

数组资源
res/values/array.xml (如果没有就手动创建)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="volume_mode">
<item>SPK</item>
<item>MIC</item>
</string-array>
</resources>

xml文件中引用

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/volume_mode"/>

代码中通过点击事件来选择进行操作

volume_mode = findViewById(R.id.spinner);
volume_mode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
String[] mode = getResources().getStringArray(R.array.volume_mode);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    
    

}
});

七、风格和主题
风格主要是指view的显示风格 res/values/filename.xml

<?xml version=1.0″ encoding=”utf-8?>
<resources>
   <style name=SpecialText” parent=@style/Text>
   <item name=”android:textSize”>18sp</item>
   <item name=”android:textColor”>#008</item>
</style>
</resources>

主题主要针对Activity等, 可以在Android Manifest中定义的和元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里.风格可以自己定义也可以使用程序自带的或是继承已有的风格。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowFrame">@drawable/screen_frame</item>
<item name="windowBackground">@drawable/screen_background_white</item>
<item name="panelForegroundColor">#FF000000</item>
<item name="panelBackgroundColor">#FFFFFFFF</item>
<item name="panelTextColor">?panelForegroundColor</item>
<item name="panelTextSize">14</item>
<item name="menuItemTextColor">?panelTextColor</item>
<item name="menuItemTextSize">?panelTextSize</item>
</style>
</resources>

样式(style)
Android样式资源文件也是入在/res/values/目录下,样式资源文件的根元素是<resources…/>元素,该元素内可包含多个<style…/>子元素,每个<style…/>
子元素定义一个样式。<style…/>指定两个属性 name:指定样式的名称,parent:指定该样式所继承的父样式。当继承某个父样式时,该样式将会获得父样式中定义的全部格式,当前样式也可以覆盖父样式中指定的格式。
<style…/>元素内可包含多个<item…/>子元素,每个<item…/>子元素定义一个格式。
res/values/style.xml (不存在即创建)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tvStyle">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#00ff00</item>
<item name="android:inputType">number</item>
</style>
<style name="subStyle" parent="tvStyle">
<item name="maxLines">3</item>
</style>
</resources>

在main activity.xml 文件中引用

<Button
android:id="@+id/testBt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
style="@style/tvStyle"
android:text="testBt" />

使用style属性可以很方便的抽取一些属性,不用重复写很多相同的属性
Theme 主题
主题与样式资的主要区别是:
1.主题不能作用于单个View组件,主体应该对整个应用中的所有Activity起作用,或对指定的Activity起作用。
2.主题定义的格式应该是改变窗口外观的格式,例如窗口标题,窗口边框等。
theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TestDrawable" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

在AndroidManifest.xml文件中引用

android:theme="@style/Theme.TestDrawable"

八、动画
动画资源分为两种,一是实现图片的translate、scale、rotate、alpha四种变化。还可以设置动画的播放特性;另一种是帧动画,逐帧播放设置的资源。
Res/anim/filename.xml //此处anim文件夹是自己建的,名字不可变。

<set xmlns:android="http://schemas.android.com/apk/res/android">               
     <translate android:interpolator="@android:anim/accelerate_interpolator"
                android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0"
                android:toYDelta="180" android:duration="2000" />
      <scale android:interpolator="@android:anim/accelerate_interpolator"
             android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0"
             android:toYScale="2.0" android:pivotX="150%" android:pivotY="150%"
             android:duration="2000" />
      <alpha android:fromAlpha="1.0" android:toAlpha="1.0"
             android:duration="@android:integer/config_mediumAnimTime" />
      <rotate ....各个属性></rotate>
     <Interpolator >可以使用其子类和属性定义动画的运行方式,先快后慢,先慢后快等</Interpolator>
</set>  

第二种资源

<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
android:oneshot=true>
<item android:drawable=@drawable/rocket_thrust1″ android:duration=200/>
<item android:drawable=@drawable/rocket_thrust2″ android:duration=200/>
<item android:drawable=@drawable/rocket_thrust3″ android:duration=200/>
</animation-list>

猜你喜欢

转载自blog.csdn.net/qq_33782617/article/details/126819569
今日推荐