Android學習——特殊底部導航欄設計

    這一次要實現的是特殊的地步導航欄,如下圖


    這裏涉及到幾個知識點,首先這裏我們用到了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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章