Android之shape与selector实现圆角

时间:2023-12-25 00:02:13

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector。可以这样说,shape和selector在美化控件中的作用是至关重要的。

1.Shape

简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

属性:

  1. <shape>  android:shape=["rectangle" | "oval" | "line" | "ring"]
  2. 其中rectagle矩形,oval椭圆,line水平直线,ring环形
  3. <shape>中子节点的常用属性:
  4. <gradient>  渐变
  5. android:startColor  起始颜色
  6. android:endColor  结束颜色
  7. android:angle  渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
  8. android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep
  9. <solid >  填充
  10. android:color  填充的颜色
  11. <stroke > 描边
  12. android:width 描边的宽度
  13. android:color 描边的颜色
  14. android:dashWidth 表示'-'横线的宽度
  15. android:dashGap 表示'-'横线之间的距离
  16. <corners > 圆角
  17. android:radius  圆角的半径 值越大角越圆
  18. android:topRightRadius  右上圆角半径
  19. android:bottomLeftRadius 右下圆角角半径
  20. android:topLeftRadius 左上圆角半径
  21. android:bottomRightRadius 左下圆角半径

2.Selector 简介位置:res/drawable/文件的名称.xml使用的方法:Java代码中:R.drawable.文件的名称XML中:android:background="@drawable/文件的名称"selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_selected="true">
  4. <shape>
  5. <gradient android:angle="270" android:endColor="#99BD4C"
  6. android:startColor="#A5D245" />
  7. <size android:height="60dp" android:width="320dp" />
  8. <corners android:radius="8dp" />
  9. </shape>
  10. </item>
  11. <item android:state_pressed="true">
  12. <shape>
  13. <gradient android:angle="270" android:endColor="#99BD4C"
  14. android:startColor="#A5D245"/>
  15. <size android:height="60dp" android:width="320dp" />
  16. <corners android:radius="8dp" />
  17. </shape>
  18. </item>
  19. <item>
  20. <shape>
  21. <gradient android:angle="270" android:endColor="#A8C3B0"
  22. android:startColor="#C6CFCE"/>
  23. <size android:height="60dp" android:width="320dp" />
  24. <corners android:radius="8dp" />
  25. </shape>
  26. </item>
  27. </selector>

list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:background="@drawable/selector"
  7. >
  8. <ImageView
  9. android:id="@+id/img"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center_vertical"
  13. android:layout_marginLeft="20dp"
  14. />
  15. <TextView
  16. android:text="data"
  17. android:id="@+id/title"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center_vertical"
  21. android:layout_marginLeft="20dp"
  22. android:layout_marginTop="20dp"
  23. android:textSize="14sp"
  24. android:textStyle="bold"
  25. android:textColor="@color/black"
  26. >
  27. </TextView>
  28. </LinearLayout>

main.xml

  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="wrap_content"
  6. android:background="#253853"
  7. >
  8. <ListView
  9. android:id="@+id/list"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:cacheColorHint="#00000000"
  13. android:divider="#2A4562"
  14. android:dividerHeight="3px"
  15. android:listSelector="#264365"
  16. android:drawSelectorOnTop="false"
  17. >
  18. </ListView>
  19. </LinearLayout>

colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="white">#FFFFFFFF</color>
  4. <color name="transparency">#00000000</color>
  5. <color name="title_bg">#1C86EE</color>
  6. <color name="end_color">#A0cfef83</color>
  7. <color name="black">#464646</color>
  8. </resources>

MainActivity

  1. package com.lingdududu.customlist;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import xb.customlist.R;
  5. import android.R.array;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ListView;
  10. import android.widget.SimpleAdapter;
  11. public class MainActivity extends Activity {
  12. ListView list;
  13. String data[] = new String[]{
  14. "China","UK","USA","Japan","German","Canada","ET","Narotu"
  15. };
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. list =(ListView) findViewById(R.id.list);
  21. SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item,
  22. new String[]{"title","img"}, new int[]{R.id.title,R.id.img});
  23. list.setAdapter(adapter);
  24. }
  25. private ArrayList<HashMap<String, Object>> getData() {
  26. ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String,Object>>();
  27. for(int i =0;i<data.length;i++){
  28. HashMap<String, Object>map = new HashMap<String, Object>();
  29. map.put("title", data[i]);
  30. map.put("img", R.drawable.item_left2);
  31. dlist.add(map);
  32. }
  33. return dlist;
  34. }
  35. }