【Android】安卓开发实战之自定义仿iPhone导航条组件
2017-02-21 11:01
357 查看
首先来看一下效果:
下面来看看实现方式:
1、首先在value文件夹下的colors.xml文件下新建两个颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="theRed">#ff6a69</color>
<color name="white">#FFFFFF</color>
</resources>2、然后在drawable文件夹下新建6个资源文件
a、round_red_food.xml
背景颜色填充为红色,左上角和左下角的弧度设为3dp,边框颜色设为白色,边框宽度设为2dp。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/theRed" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="3dp"
/>
<stroke
android:width="2dp"
android:color="@color/white"
/>
</shape> b、round_white_food.xml
背景颜色填充为白色,左上角和左下角的弧度设为3dp,不设置边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="3dp"
/>
</shape> c、round_red_out.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--第一个item是最底层的布局-->
<item>
<!--这个布局的形状设置为方框-->
<shape android:shape="rectangle">
<!--给底层画一个宽度为10dp的白色方框-->
<stroke
android:width="10dp"
android:color="@color/white" />
<!--这个padding挤压的是第二层的布局空间
所以才会形成上下是白色边框,中间是红色填充的效果
-->
<padding
android:top="2dp"
android:bottom="2dp" />
</shape>
</item>
<!--这是第二层布局布局
设置为方形,以红色填充
-->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/theRed" />
</shape>
</item>
</layer-list>
d、round_white_out.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:background="@color/theRed"
tools:context="com.example.test.MainActivity">
<!--在background里设置就好-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_white_food"
android:text="美食"
android:textColor="@color/theRed"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_red_out"
android:text="出行"
android:textColor="@color/white"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_red_hotel"
android:text="酒店"
android:textColor="@color/white"
android:layout_marginTop="10dp"
/>
</LinearLayout>
最后看一下效果图:
下面来看看实现方式:
1、首先在value文件夹下的colors.xml文件下新建两个颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="theRed">#ff6a69</color>
<color name="white">#FFFFFF</color>
</resources>2、然后在drawable文件夹下新建6个资源文件
a、round_red_food.xml
背景颜色填充为红色,左上角和左下角的弧度设为3dp,边框颜色设为白色,边框宽度设为2dp。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/theRed" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="3dp"
/>
<stroke
android:width="2dp"
android:color="@color/white"
/>
</shape> b、round_white_food.xml
背景颜色填充为白色,左上角和左下角的弧度设为3dp,不设置边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="3dp"
/>
</shape> c、round_red_out.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--第一个item是最底层的布局-->
<item>
<!--这个布局的形状设置为方框-->
<shape android:shape="rectangle">
<!--给底层画一个宽度为10dp的白色方框-->
<stroke
android:width="10dp"
android:color="@color/white" />
<!--这个padding挤压的是第二层的布局空间
所以才会形成上下是白色边框,中间是红色填充的效果
-->
<padding
android:top="2dp"
android:bottom="2dp" />
</shape>
</item>
<!--这是第二层布局布局
设置为方形,以红色填充
-->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/theRed" />
</shape>
</item>
</layer-list>
d、round_white_out.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="@color/white" /> <padding android:top="2dp" android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="@color/white" /> </shape> </item> </layer-list>e、round_red_hotel.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/theRed" /> <corners android:topLeftRadius="0dp" android:topRightRadius="3dp" android:bottomRightRadius="3dp" android:bottomLeftRadius="0dp" /> <stroke android:width="2dp" android:color="@color/white" /> </shape>f、round_white_hotel.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white" /> <corners android:topLeftRadius="0dp" android:topRightRadius="3dp" android:bottomRightRadius="3dp" android:bottomLeftRadius="0dp" /> </shape>3、在activity_main.xml布局中引用这几个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:background="@color/theRed"
tools:context="com.example.test.MainActivity">
<!--在background里设置就好-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_white_food"
android:text="美食"
android:textColor="@color/theRed"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_red_out"
android:text="出行"
android:textColor="@color/white"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_red_hotel"
android:text="酒店"
android:textColor="@color/white"
android:layout_marginTop="10dp"
/>
</LinearLayout>
最后看一下效果图:
相关文章推荐
- 【Android】安卓开发实战之仿iPhone通讯录demo的移植和优化
- 【Android】安卓开发实战之自定义EditText输入框形状颜色
- 【Android】安卓开发实战之自定义ActionBar(活动栏、状态栏)颜色且没有色差
- 【Android】安卓开发实战之自定义分隔线条的长度、宽度、颜色
- 【Android】安卓开发实战之自定义对话框位置、大小、颜色、形状
- 【Android 应用开发】 自定义 圆形进度条 组件
- Android开发——构建自定义组件
- Android开发之自定义评分组件
- 【android 开发知识积累】——属性(Attribute)资源的使用和自定义View组件
- Android-开发自定义组件
- Android开发指南-用户界面-创建自定义组件
- Android开发指南-用户界面-创建自定义组件
- Android开发--打包自定义组件成JAR包
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- Android开发实战2----圆点导航指示器(使用自定义View实现)
- Android项目开发实战—自定义左右菜单
- 【Android 应用开发】 自定义 圆形进度条 组件
- 【一步一步,从无到有 --- 安卓项目实战】 Android开发环境的安装
- 【android开发记录片】2.自定义/定制 Dialog组件
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件