Android中引用XML文件
来源:互联网 发布:淘宝大韩泡泡糖靠谱吗 编辑:程序博客网 时间:2024/06/16 07:32
Android在UI布局中经常会引用到很多Xml文件,我们不能老是去查找这些标签如编写,做着重复的工作。
现将经常使用的做一下归纳,方便自己的记忆和理解
一、布局文件:在layout目录下,使用比较广泛;
我们可以为应用定义两套或多套布局,例如:可以新建目录layout_land(代表手机横屏布局),layout_port(代表手机竖屏布局),系统会根据不同情况自动找到最合适的布局文件,但是在同一界面的两套不同布局文件的文件名应该是相同的,只是放在了两个不同的目录下。
二、图片文件:在drawable目录下,从2.1版本以后分为三个目录,
drawable-hdpi里面存放高分辨率的图片,如WVGA (480×800),FWVGA (480×854)
drawable-mdpi里面存放中等分辨率的图片,如HVGA (320×480)
drawable-ldpi里面存放低分辨率的图片,如QVGA (240×320)
系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。
在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。
- 我们可以将已经做好的图片放到该目录下,或者通过自定义XML文件来实现想要的图片,例如我们可以定义shapge_1.xml放到drawable目录下,内容如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 --> <corners android:radius="9dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" android:bottomLeftRadius="2dp" android:bottomRightRadius="2dp"/><!-- 设置圆角半径 --> <!-- 渐变 --> <gradient android:startColor="@android:color/white" android:centerColor="@android:color/black" android:endColor="@android:color/black" android:useLevel="true" android:angle="45" android:type="radial" android:centerX="0" android:centerY="0" android:gradientRadius="90"/> <!-- 间隔 --> <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp"/><!-- 各方向的间隔 --> <!-- 大小 --> <size android:width="50dp" android:height="50dp"/><!-- 宽度和高度 --> <!-- 填充 --> <solid android:color="@android:color/white"/><!-- 填充的颜色 --> <!-- 描边 --> <stroke android:width="2dp" android:color="@android:color/black" android:dashWidth="1dp" android:dashGap="2dp"/></shape>填充:设置填充的颜色间隔:设置四个方向上的间隔大小:设置大小圆角:同时设置五个属性,则Radius属性无效android:Radius="20dp" 设置四个角的半径android:topLeftRadius="20dp" 设置左上角的半径 android:topRightRadius="20dp" 设置右上角的半径 android:bottomLeftRadius="20dp" 设置右下角的半径 android:bottomRightRadius="20dp" 设置左下角的半径描边:dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框android:width="20dp" 设置边边的宽度 android:color="@android:color/black" 设置边边的颜色 android:dashWidth="2dp" 设置虚线的宽度 android:dashGap="20dp" 设置虚线的间隔宽度渐变:当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错。android:useLevel 这个属性不知道有什么用。
- 当我们想让一个控件根据不同状态显示不同图片,可以直接在程序中控制,也可以在drawable目录建立XML文件达到相同的效果,例如:我们可以在drawable目录下新建文件button_back.xml
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"android:drawable="@drawable/xxx1" /> <item android:state_pressed="true" android:drawable="@drawable/xxx2" /> <item android:state_focused="true" android:drawable="@drawable/xxx3" /> <-- 这里还可以加N多效果和动作 只要你用的到 --> <item android:drawable="@drawable/xxx4" /></selector>
以上XML文件可以实现一个控件(假设为button),获取焦点,按下按钮,正常状态下显示不同图片的效果,只需要在定义控件是引用该文件名即可,例如:
<Button android:id="@+id/Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_add_x"></Button><!--android:background="@drawable/button_back"指向button_back.xml文件-->
但是当我们的条件不是系统已有的事件类型,例如根据ImageView根据一个变量var的值的不同显示不同的图片,应该怎么办呢?可以在程序中写如下代码
if (条件1){ image.setBackground(R.id.xxx1); }else if (条件2){ image.setBackground(R.id.xxx2); } ...
或者可以用另一个简便的方法实现相同的功能,在res/drawable下建立一个xml文件,内容如下
<level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:maxLevel="9" android:drawable="@drawable/battery_0" /> <item android:maxLevel="39" android:drawable="@drawable/battery_1" /> <item android:maxLevel="69" android:drawable="@drawable/battery_2" /> <item android:maxLevel="89" android:drawable="@drawable/battery_3" /> <item android:maxLevel="100" android:drawable="@drawable/battery_4" /></level-list>
然后在layout中把imageview的src设置成已创建好的xml文件 ,程序中变换图片时,只需要使用 imageview.getDrawable().setLevel(50);
Android会根据level的值自动选择对应的图片。手机显示剩余电量就是用这个方法来显示不同图片的。
3、菜单文件:在menu目录下,写代码时只需在onCreateOptionsMenu方法中用MenuInflater装载进去就OK了。格式如下,
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/enabled_item" android:title="Enabled" android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item" android:title="Disabled" android:enabled="false" android:icon="@drawable/stat_sad" /> <item android:id="@+id/enabled_item_2" android:title="Enabled" android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item_2" android:title="Disabled" android:enabled="false" android:icon="@drawable/stat_sad" /></menu>
四、resource文件,在values目录下,之所以称之为resource文件,是因为values目录下xml文件都是以resource作为根节点
strings.xml 定义字符串的文件,格式如下:
<resources> <string name="hello">Hello World!</string> <string name="app_name">我的应用程序</string></resources>
colors.xml 定义颜色的文件,格式如下:
<resources> <!--定义图片颜色--> <drawable name="screen_background_black">#ff000000</drawable> <drawable name="translucent_background">#e0000000</drawable> <drawable name="transparent_background">#00000000</drawable> <!--定义文字颜色--> <color name="solid_red">#f00</color> <color name="solid_blue">#0000ff</color> <color name="solid_green">#f0f0</color> <color name="solid_yellow">#ffffff00</color></resources>
arrays.xml 定义数组的文件,格式如下:
<resources> <string-array name="planets"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> <item>Pluto</item> </string-array> <integer-array name="numbers"> <item>100</item> <item>500</item> <item>800</item> </integer-array></resources>
styles.xml 定义样式的文件,分为两种用途:
Style:以一个单位的方式用在布局XML单个元素(控件)当中。 例如:我们可以为TextView定义一种样式风格,包含文本的字号大小和颜色,然后将其用在TextView特定的实例。
Theme:以一个单位的方式用在应用中所有的Activity当中或者应用中的某个 Activity当中。 比如,我们可以定义一个Theme,它为window frame和panel 的前景和背景定义了一组颜色,并 为菜单定义可文字的大小和颜色属性,可以将这个Theme应用在你程序当中所有的Activity里。
<resources> <!--Theme,可以用来定义activity的主题--> <style name="Theme.Transparent"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <item name="android:windowBackground">@drawable/transparent_background</item> <item name="android:windowNoTitle">true</item> <item name="android:colorForeground">#fff</item> </style> <!--Style,可以用来定义某个View元素,这里是ImageView的样式--> <style name="ImageView120dpi"> <item name="android:src">@drawable/stylogo120dpi</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style></resources>
个人认为,其实不管是Theme还是Style,其实只是应用的范围不同而已,区分的话应该是根据android:name=”xxxx”的xxxx来区分吧,很明显是不同的。
dimen.xml 定义单位的文件,android中度量单位有以下几种:
px(象素): 屏幕实际的象素,常说的分辨率1024*768pixels,就是横向1024px, 纵向768px,不同设备显示效果相同。
in(英寸): 屏幕的物理尺寸, 每英寸等于2.54厘米。
mm(毫米): 屏幕的物理尺寸。
pt(点) : 屏幕的物理尺寸。1/72英寸。
dp/dip : 与密度无关的象素,一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。但dp和px的比例会随着屏幕密度的变化而改变,不同设备有不同的显示效果。
sp : 与刻度无关的象素,主要用于字体显示best for textsize,作为和文字相关大小单位。
<?xml version="1.0" encoding="utf-8"?><declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable>
attrs.xml 可以自己定义属性,而不用系统自带的属性
可在values中创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> </resources>
如自定义View的时候引用
public class MyView extends View { private Paint mPaint; private static final String mString = "Welcome to BaiYe's blog"; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); float textSize = a.getDimension(R.styleable.MyView_textSize, 36); mPaint.setTextSize(textSize); mPaint.setColor(textColor); a.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 设置填充 mPaint.setStyle(Paint.Style.FILL); // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标// mPaint.setColor(Color.BLACK); canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); // 绘制文字 canvas.drawText(mString, 60, 410, mPaint); }}
- 首先从 R.styleable.CustomView 获得了TypedArray变量
- 再用getColor(),getDimension()等方法获取相应的属性值,属性格式为“样式名_属性名”,属性后面的* 参数是默认值。
- 获得属性值以后,就可以应用这些属性值。
- recycle()方法用于返回信号给资源(不懂什么意思)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:test="http://schemas.android.com/apk/res-auto"//一定记得添加前缀 android:id="@+id/activity_attrs_actiity" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lizi.newset.CustomView.attrs.AttrsActivity"> <com.lizi.newset.CustomView.attrs.MyView android:id="@+id/myView" android:layout_height="match_parent" android:layout_width="wrap_content" test:textSize="50px" test:textColor="#ff00ff"/> /></RelativeLayout>
xmlns:test=”http://schemas.android.com/apk/res-auto”一定要添加,添加之后才能在xml中自定义属性
- Android中引用XML文件
- android 在xml文件中引用自定义View
- android 在xml文件中引用自定义View
- Android XML文件中引用资源的方法
- android 在xml文件中引用自定义View
- android Xml文件引用资源中@和?的区别
- android之xml文件中特殊符号的引用
- Android XML 文件中引用资源的方法
- Android xml资源文件中@、@android:type、@*、?、@+引用写法含义以及区别
- Android xml资源文件中@、@android:type、@*、?、@+引用写法含义以及区别
- Android xml资源文件中@、@android:type、@*、?、@+引用写法含义以及区别
- Android在XML文件中或者java程序中引用资源的方法
- Android在XML文件中或者Java程序中引用资源的方法
- android中XML文件
- Android 在XML中引用系统资源方法
- Android--入门---19---(XML文件中引用资源的语法)
- 如何在web.xml中引用其它的xml文件
- android 文字引用排版 String.xml中文字引用格式
- PHP生成验证码的类
- Leetcode-Insert Interval
- Python的操作性函数
- ORACLE TRUNC()函数整理
- Markdown里面使用mermaid画流程图(基础)
- Android中引用XML文件
- MT6795 Video Feature Introduction
- js中push(),pop(),unshift(),shift()的用法小结
- Unable to compile class for JSP问题的解决方案
- 安卓 Activity 生命周期
- Python递归调用_汉诺塔问题
- 使用python切片实现二维数组复制
- 实现换肤的前章:Android资源管理机制
- Nginx tomcat SSL证书与Https应用部署