1.设置窗口的背景:getWindow().setBackgroundDrawableResource(R.drawable.back);
2.设置窗体的标题:getWindow().setText(getResources().getText(R.string.main_title));
3.<?xml version="1.0" encoding="utf-8">
<resources>
<color name="red">#ffff0000</color>
<string name="hello">hello</string>
<dimen name="spacing">8dip</dimen>
<bool name="is_true">true</bool>
<integer name="book_number">12</integer>
</resources>
android:textColor="@color/red"
android:text="@string/hello"
android:textSize="@dimen/spacing"
text.setWidth((int)getResources().getDimension(R.dimen.spacing));
boolean is_true=getResources().getBoolean(R.bool.is_true);
int book_number=getResources().getInteger(R.integer.book_number);
4.数组资源:
<!--普通类型的数组 例如Drawable数组 TypedArray obtainTypedArray(int id) 根据普通数组资源的名称来获取普通数组-->
<array name="plain_arr">
<item>@color/c1</item>
</array>
<!--字符串数组 通过 String[]getStringArray(int id) 获取字符串数组-->
<string-array name="string_arr">
<item>@string/c1</item>
<item>谢丹替补</item>
</string-array>
<!--整形数组 通过 int[]getIntegerArray(int id)获取实际的整形数组-->
<integer-array name="book-num">
<item>1</item>
</integer-array>
typedArray icons=getResources().obtainTypedArray(R.array.plain_arr);
text.setBackgroundDrawable(icons.getDrawable(position));
5 Drawable资源
5.1 StateListDrawable
<?xml version="1.0" encoding="utf-8">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color/drawable="***" android:state_pressed="true|false"/>
</selector>
5.2 LayerDrawable
<?xml version="1.0" encoding="utf-8">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/grow"></item>
<item android:id="@android:id/background" android:drawable="@drawable/progress"></item>
<layer-list>
5.3 ShapeDrawable
<?xml version="1.0" encoding="utf-8">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle(矩形)|oval(椭圆形)|line(线)|
ring(圆形)">
<!--定义几何图形的四个角的弧度-->
<corners android:radius(半径)=""
android:topLeftRadius=""/>
<!--定义使用渐变色填充-->
<gradient android:angle(坡度)="integer"
android:centerX|centerY="integer"
android:centerColor="color"
android:startColor="color"
android:endColor="color"
android:gradientRadius="integer"
android:type="linear(线性)|radial(放射状)|sweep(曲线)"
android:userLevel="true|false"/>
<!--定义几何形状的内边距-->
<padding android:left|top|right|bottom=""/>
<!--定义几何形状的大小-->
<size android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer"/>
<!--定义使用单种颜色填充-->
<solid android:color="color"/>
<!--定义为几何形状绘制边框-->
<stroke(画笔) android:width="integer"
android:color=""
android:dashWidth=""
android:dashGap="" />
</shape>
5.4 ClipDrawable
<?xml version="1.0" encoding="utf-8">
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/**"
android:clipOrientation="horizontal|vertical"(截取方向)
android:gravity=""(截取时的对齐方向)>
</clip>
ClipDrawable的对象可以调用setLevel(int)设置截取区域大小 0:空 10000:整张图片
ImageView imageview=(ImageView)findViewById(**)
final ClipDrawable drawable=(ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel+20);
5.5 AnimationDrawable 资源
<?xml version="1.0" encoding="utf-8">
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator|accelerate_interpolator|decelerate_interpolator"
android:shareInterpolator(以下所有元素是否使用相同的动画速度)="true|false">
<alpha android:fromAlpha=""
android:toAlpha=""/>
<scale android:fromXScale=""
android:toXScale=""
android:fromYScale=""
android:toYScale=""
android:pivotX=""(缩放的中心点)
android:pivotY=""/>
<translate(平移) android:fromX=""
android:toX=""
android:fromY=""
android:toY=""/>
<rotate(旋转) android:fromDegrees=""
android:toDegrees=""
android:pivotX=""
android:pivotY=""/>
</set>
备注:那个元素在前面就先运行哪个元素
final ImageView image=....;
final Animation anim=AnimationUtils.loadAnimation(this,R.anim.my_anim);
//设置动画结束后保留结束状态
anim.setFillAfter(true);
image.startAnimation(anim);
6 Style(样式) 放在res/values
<?xml version="1.0" encoding="utf-8">
<resources>
<style name="style1">
<item name="android:textSize">20sp</item>
</style>
<style name="style2" parent="@style/style1">
<item name="android:textSize">30sp</item><!--覆盖父元素-->
</style>
</resources>
<EdtiText style="@style/style2"/>
7.Theme主题
<?xml version="1.0" encoding="utf-8">
<resources>
<style name="xlm">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowFrame">@drawable/window_border</item>
<item name="android:windowBackground">@drawable/start</item>
</style>
</resources>
1.setTheme(android.R.style.xlm);//java
2.<application android:theme="@style/xlm">
3.<activity android:theme="@style/xlm">
8 Attribute 属性资源
放在res/values
<?xml version="1.0" encoding="utf-8">
<resources>
<attr name="xlm">
</attr>
<!--定义一个styleable对象来组合多个属性-->
<declare-styleable name="xiedantibu">
<attr name="xlm">
</declare-styleable>
</resources>
TypedArray typedarray=context.obtainStyleAttributes(attrs,R.styleable.xiedantibu)
int xlm=typedarray.getInt(R.styleable.xiedantibu_xlm);
xmlns:xiedantibu="http://schemas.android.com/apk/res/项目包名"
xiedantibu:xlm="1000"
9.使用原始资源
1.在res/raw目录中放的资源: inputStream is=context.getResources().openRawResource(R.drawable.*);
2.位于/assets目录下的资源 1.AssetManager am=getAssets(); 2.InputStream open(String fileName)|(AssetFileDescirptor openFd(String fileName) -> AssetFileDescirptor.getFileDescriptor());
809-android应用资源
最新推荐文章于 2022-10-02 09:08:43 发布