【Android】安卓开发实战之自定义仿iPhone导航条组件

首先来看一下效果:


下面来看看实现方式:

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>

最后看一下效果图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值