UI效果(2): shape

来源:互联网 发布:最优化理论与算法 答案 编辑:程序博客网 时间:2024/06/03 02:26

android中的控件不是很多,布局也很少。但是这些东西很灵活,所以想设计好的界面还是需要一定的功底的。

本篇博客,您可以了解到:

<1> 自定义 Toast

<2> Button特殊效果

在sdk的api中,相信你可以找到名为Shape Drawable的这样一篇文章,位置:


如果,你想更加理解掌握这篇博客,建议好好地、仔细地看看这篇Shape Drawable文章。

秀一下效果图,呵呵!

启动应用时候,第一次出现效果:


让该Button获得焦点,效果:


还有,就是按下Button会改变其颜色(红色)并弹出Toast:



如果,你是一个细心的人就会发现Toast与以往显示的位置不一样,呵呵。代码如下:

[java] view plaincopyprint?
  1. findViewById(R.id.btn_change).setOnClickListener(new OnClickListener() {  
  2.               
  3.             @Override  
  4.             public void onClick(View v) {  
  5.                 Toast toast = Toast.makeText(DroidButtonActivity.this"changed", Toast.LENGTH_LONG);  
  6.                 toast.setGravity(Gravity.CENTER, 00);  
  7.                 toast.show();  
  8.             }  
  9.         });  
这里面实现Button特殊效果需要selector,一个很cool的xml文件,在/res/drawable下面:


那么,看看该xml文件内容吧!?

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5.     <item android:state_pressed="true">  
  6.   
  7.         <shape>  
  8.   
  9.             <gradient android:startColor="#bb0000" android:endColor="#cc0000"  
  10.   
  11.                 android:angle="270" />  
  12.   
  13.             <stroke android:width="3dp" android:color="#aa0000" />  
  14.   
  15.             <corners android:radius="2dp" />  
  16.   
  17.             <padding android:left="10dp" android:top="10dp"  
  18.   
  19.                 android:right="10dp" android:bottom="10dp" />  
  20.   
  21.         </shape>  
  22.   
  23.     </item>  
  24.   
  25.     <item android:state_focused="true">  
  26.   
  27.         <shape>  
  28.   
  29.             <gradient android:startColor="#00aa00" android:endColor="#00bb00"  
  30.   
  31.                 android:angle="270" />  
  32.   
  33.             <stroke android:width="2dp" android:color="#00cc00" />  
  34.   
  35.             <corners android:radius="2dp" />  
  36.   
  37.             <padding android:left="10dp" android:top="10dp"  
  38.   
  39.                 android:right="10dp" android:bottom="10dp" />  
  40.   
  41.         </shape>  
  42.   
  43.     </item>  
  44.   
  45.     <item>  
  46.   
  47.         <shape>  
  48.   
  49.             <gradient android:startColor="#0000bb" android:endColor="#0000cc"  
  50.   
  51.                 android:angle="270" />  
  52.   
  53.             <stroke android:width="2dp" android:color="#0000aa" />  
  54.   
  55.             <corners android:radius="2dp" />  
  56.   
  57.             <padding android:left="10dp" android:top="10dp"  
  58.   
  59.                 android:right="10dp" android:bottom="10dp" />  
  60.   
  61.         </shape>  
  62.   
  63.     </item>  
  64.   
  65. </selector>  
关于各个参数的意思,看上面的sdk文档。在main.xml文件中,我们就可以来使用该xml文件喽!

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     />  
  11. <Button   
  12.     android:id="@+id/btn_change"  
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:layout_marginTop="10dip"  
  16.     android:text="change"  
  17.     android:background="@drawable/an_button"/>  
  18. </LinearLayout>  
注意:Button设置selector使用标签是background,不是src(区别于ImageButton)。

完整代码下载:http://download.csdn.net/source/3469895

转自:http://blog.csdn.net/veryitman/article/details/6633970

0 0
原创粉丝点击