android 小常识(包含一些界面开发的推荐颜色和自定义按钮~)

来源:互联网 发布:网络规划与设计 编辑:程序博客网 时间:2024/06/05 09:27

转自:http://android.yaohuiji.com/archives/299


android2.2开始增加了match_parent这个属性,这个属性和原来的fill_parent一样,

如果作程序时,向考虑自己的程序向下兼容的话,就要用fill_parent,


下面是两者相同的证据,看android.view.ViewGroup里的静态嵌套类LayoutParams中的代码:

[java] view plaincopy
  1. public static final int FILL_PARENT = -1;  
  2.    /** 
  3.     * Special value for the height or width requested by a View. 
  4.     * MATCH_PARENT means that the view wants to be as big as its parent, 
  5.     * minus the parent's padding, if any. Introduced in API Level 8. 
  6.     */  
  7.    public static final int MATCH_PARENT = -1;  
  8.    
  9.    /** 
  10.     * Special value for the height or width requested by a View. 
  11.     * WRAP_CONTENT means that the view wants to be just large enough to fit 
  12.     * its own internal content, taking its own padding into account. 
  13.     */  

图是帮助文档中的关于界面开发的推荐色值,大家可以参考一下:




android:gravity和android:layout_gravity区别


从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。



在Android中使用自定义图片按钮


(本节内容基本上是官方文档的翻译,看过官方文档的朋友可以直接略过。) 
在这一节里,我们将使用一个button组件,一个xml文件和三个图片来制作一个自定义的按钮,当按钮按下的时候会有一个气泡消息弹出来。

1、建立一个Android项目,拷贝这三张图到 res/drawable目录下,这三张图用来表述按钮的三种状态。

android_focusedandroid_normalandroid_pressed

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中增加,下面的代码:

view sourceprint?
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();
            } 
        });

这样点击按钮就会显示”哔,哔,哔!”了。下图是最终运行效果:

image

最后总结一下,使用selector自定义xml,设置button的背景属性为这个自定义xml即可实现不同状态换不同的图像。