您的位置:首页 > 移动开发 > Android开发

【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"?>
<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>
最后看一下效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐