自定义ActionBar View

来源:互联网 发布:mysql sum group by 编辑:程序博客网 时间:2024/06/07 01:22

1.布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="horizontal"              android:layout_width="fill_parent"              android:gravity="center"              android:background="@color/colorPrimary"              android:layout_height="fill_parent">    <Button        android:id="@+id/btn"        android:drawableLeft="@drawable/user_default_face"        android:layout_marginLeft="3dp"        android:drawablePadding="3dp"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:textColor="@color/white"        android:background="@null"        android:text="用户"        android:gravity="center"        android:onClick="clickEvent"        android:layout_height="wrap_content"/>    <ImageButton        android:background="@null"        android:layout_marginLeft="150dp"        android:src="@drawable/action_bar_icon_search"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <ImageButton        android:background="@null"        android:layout_marginLeft="15dp"        android:src="@drawable/action_bar_icon_add"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <ImageButton        android:background="@null"        android:layout_marginLeft="15dp"        android:src="@drawable/action_bar_icon_more"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>


2.对ActionBar属性进行设定

       actionBar.setDisplayHomeAsUpEnabled(false);        actionBar.setDisplayShowTitleEnabled(false);        actionBar.setDisplayShowCustomEnabled(true);        actionBar.setDisplayUseLogoEnabled(false);        actionBar.setHomeButtonEnabled(false);        View view = getLayoutInflater().inflate(R.layout.main_actionbar,null);        actionBar.setCustomView(view);

3.添加点击事件

要在需添加事件的控件,xml文件里加上

        android:onClick="clickEvent"

public void clickEvent(View view){        if (view.getId() == R.id.btn){            Intent intent = new Intent(MainActivity.this, UserActivity.class);            startActivity(intent);        }    }


4.实现效果




0 0
原创粉丝点击