Android学习——特殊底部导航栏设计

本文介绍了如何在Android中设计一个特殊的底部导航栏,使用了ImageButton和FloatingActionButton控件,并通过Photoshop处理透明背景图片。在布局代码中,通过dimens.xml文件设置了FloatingActionButton的大小。在主类中完成了相关配置。这是一个适合初学者的简单示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    这一次要实现的是特殊的地步导航栏,如下图


    这里涉及到几个知识点,首先这里我们用到了ImageButton和FloatingActionButton控件,这里准备了四张图片作为按键的图片素材,都用PhotoShop处理成透明背景图片。


    接下来是布局代码

    

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.drw.myapplication.MainActivity"
    android:background="#DDD">
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        app:fabSize="normal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="#fff"
        >
        <ImageButton
            android:id="@+id/first"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:padding="20dp"
            android:background="@mipmap/first_selected"
            android:onClick="first"
            />
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:visibility="invisible"/>
        <ImageButton
            android:id="@+id/second"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:padding="20dp"
            android:background="@mipmap/second_unselected"
            android:onClick="second"/>
    </LinearLayout>
</RelativeLayout>

    FloatingActionButton的大小设置需要创建dimens.xml文件,并输入如下代码,这里的80dp是需要设置的大小,注意这里是覆盖了源码对fabsize=“normal”中normal代表的大小

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="design_fab_size_normal">80dp</dimen>
</resources>

    然后是主类

public class MainActivity extends AppCompatActivity {
    boolean first_selected=false;
    boolean second_selected=false;
    ImageButton first;
    ImageButton second;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first=(ImageButton)findViewById(R.id.first);
        second=(ImageButton)findViewById(R.id.second);
    }

    public void first(View v){
        if(first_selected==true) return;
        second.setBackgroundResource(R.mipmap.second_unselected);
        first.setBackgroundResource(R.mipmap.first_selected);
        first_selected=true;
        second_selected=false;
    }

    public void second(View v){
        if(second_selected==true) return;
        first.setBackgroundResource(R.mipmap.first_unselected);
        second.setBackgroundResource(R.mipmap.second_selected);
        first_selected=false;
        second_selected=true;
    }
}


   好了,这个简单的例子就到这里了,我是菜鸟,多多指教。DRW

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值