android xml注意事项
初次接触android开发时,xml文件的编写入手很快,但依旧存在需要注意的问题,记载下来,方便记忆。
1.xml文件名貌似不能兼容大写字母
2.编写xml文件时,需要考虑兼容性,记在不同大小的屏幕大小的手机上布局都正常显示
<ImageButton android:id="@+id/imgBtnNewTask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"//从左开始,右的话也是相应的,假如你要想一个控件放在右边,就可以用相应的右属性,接下来就可以用下面的距离右多少个像素点之类的 android:layout_centerVertical="true" //居中 android:layout_marginLeft="25dp"//如果是右的话就要使layout_alignParentRight这个属性为真才有作用,同时,还必须是RelativeLayout布局 android:layout_marginTop="15dp" //可靠Top和Bottom这两个属性来使得背景色的大小协调一下 android:layout_marginBottom="15dp" android:background="@color/topColor" android:src="@drawable/plus"/>
由此可见,不要使用那些android:layout_height="100dp"这样的语句,这样在开发中就不能兼容其他机子了
3.对于dp、px、sp等基本单位的说明
dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。
由此,根据 google 的建议,TextView 的字号最好使用 sp 做单位,而且查看TextView的源码可知 Android 默认使用 sp 作为字号单位。
还有, google代码里边所用的单位都是以px为默认单位的.
4.gravity和layout_gravity属性的区别
android:gravity是用来设置控件里面的子空间等对齐方式的;例如butoon中文字靠右。
android:layout_gravity是用来设置控件本身在它的父控件中的对齐方式
5.控件隐藏
android:visibility参数为VISIBLE:可见的,值为0;参数为INVISIBLE:不可见,但占据一定空间布局,值4;参数为GONE:不可见,不占据任何控件布局,值8。如:
text1.setVisibility(View.VISIBLE);
6.ExpandableListView颜色属性
滑动ExpandableListView时,会变成默认的黑色,为了使它不变色,可以用以下这句话,加入你的背景色即可:
android:cacheColorHint="@color/white"
以下这句是点击ExpandabListView中的选项时颜色的变化
android:listSelector="#F7F7F7"
7.listView样式
final ListAdapter msListAdaper = new ArrayAdapter<String>(AlterTypeActivity.this, android.R.layout.select_dialog_singlechoice, msList);
上面语句中的android.R.layout.select_dialog_singlechoice是样式之一,还有android.R.layout.simple_expandable_list_item_1之类的样式,除此之外,我们也可换成自己的xml。但是需要注意的是这个xml文件仅能有一个textview。连Layout也不能有。否则会报错:ArrayAdapter requires the resource ID to be a TextView。如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" > </TextView>
以上是本人初次接触android时发现的一些注意事项,由于新手,如有不对之处,恳请指出。
更多相关文章
- Android(安卓)通过selector改变状态
- Android(安卓)自定义View——自定义ProgressBar
- [置顶] 我的Android进阶之旅------>Android中android:visibility
- 关于Android的Animation使用(XML)
- Android中设置半个屏幕大小且居中的按钮布局 (layout_weight属性
- android开发老罗教学视频
- android的动画相关参数说明
- 【android】scaleType属性与ImagView中图片的显示的关系
- Android开发艺术探索笔记 第四章 View的工作原理