android 小常识(包含一些界面开发的推荐颜色和自定义按钮~)
来源:互联网 发布:网络规划与设计 编辑:程序博客网 时间:2024/06/05 09:27
转自:http://android.yaohuiji.com/archives/299
android2.2开始增加了match_parent这个属性,这个属性和原来的fill_parent一样,
如果作程序时,向考虑自己的程序向下兼容的话,就要用fill_parent,
下面是两者相同的证据,看android.view.ViewGroup里的静态嵌套类LayoutParams中的代码:
- public static final int FILL_PARENT = -1;
- /**
- * Special value for the height or width requested by a View.
- * MATCH_PARENT means that the view wants to be as big as its parent,
- * minus the parent's padding, if any. Introduced in API Level 8.
- */
- public static final int MATCH_PARENT = -1;
- /**
- * Special value for the height or width requested by a View.
- * WRAP_CONTENT means that the view wants to be just large enough to fit
- * its own internal content, taking its own padding into account.
- */
图是帮助文档中的关于界面开发的推荐色值,大家可以参考一下:
android:gravity和android:layout_gravity区别
从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。
在Android中使用自定义图片按钮
(本节内容基本上是官方文档的翻译,看过官方文档的朋友可以直接略过。)
在这一节里,我们将使用一个button组件,一个xml文件和三个图片来制作一个自定义的按钮,当按钮按下的时候会有一个气泡消息弹出来。
1、建立一个Android项目,拷贝这三张图到 res/drawable目录下,这三张图用来表述按钮的三种状态。
2、在res/drawable目录下建立一个android_button.xml文件,其内容如下:
<xml version=”1.0″ encoding=”utf-8”>
<selector xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:state_pressed=”true” android:drawable=”@drawable/android_pressed” />
<item android:drawable=”@drawable/android_focused” android:state_focused=”true” />
<item android:drawable=”@drawable/android_normal” />
</selector>
注意,xml中的三个item之间是有严格顺序关系的,只有前两个不为真时才会选择第三个item,如果第三个放在了第一位,那么后面两个定义了也是白定义,你不信可以换一下顺序看看。
3、在main.xml中增加,下面的代码:
1
<button android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:id=
"@+id/button"
android:background=
"@drawable/android_button"
android:padding=
"10dp"
></button>
4、在Activity的OnCreate()中添加如下代码:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainCustomButton.this, “哔,哔,哔!”, Toast.LENGTH_SHORT).show();
}
});
这样点击按钮就会显示”哔,哔,哔!”了。下图是最终运行效果:
最后总结一下,使用selector自定义xml,设置button的背景属性为这个自定义xml即可实现不同状态换不同的图像。
- android 小常识(包含一些界面开发的推荐颜色和自定义按钮~)
- android 小常识(包含一些界面开发的推荐颜色和自定义按钮~)
- android 小常识(包含一些界面开发的推荐颜色和自定义按钮~) .
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android界面开发推荐颜色
- Android UI---界面开发推荐颜色
- Android UI---界面开发推荐颜色
- Android进阶(二十四)Android UI---界面开发推荐颜色
- 【UI color】Android UI---界面开发推荐颜色
- 一些android的常识
- android的一些常识
- 一些路径的小常识
- yaffs格式根文件系统制作
- spring的view
- JAVA I/O流 学习
- 通过nfs服务挂载根文件系统
- Android 关于 如何使用外界导入的数据库文件
- android 小常识(包含一些界面开发的推荐颜色和自定义按钮~)
- YAFFS2概述
- Message,MessageQueue,Looper,Handler详解
- Intent大全,随用随更新
- yaffs 文件系统移植
- 利用Sensor实现android手机类似于微信摇一摇
- 数字图像基础,论坛,算法库matlab,opencv,halcon
- C语言字符串处理函数
- Android控件常用属性