自定义ActionBar View

╰半夏微凉° 2022-09-25 10:29 272阅读 0赞

1.布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:gravity="center"
  6. android:background="@color/colorPrimary"
  7. android:layout_height="fill_parent">
  8. <Button
  9. android:id="@+id/btn"
  10. android:drawableLeft="@drawable/user_default_face"
  11. android:layout_marginLeft="3dp"
  12. android:drawablePadding="3dp"
  13. android:layout_gravity="center"
  14. android:layout_width="wrap_content"
  15. android:textColor="@color/white"
  16. android:background="@null"
  17. android:text="用户"
  18. android:gravity="center"
  19. android:onClick="clickEvent"
  20. android:layout_height="wrap_content"/>
  21. <ImageButton
  22. android:background="@null"
  23. android:layout_marginLeft="150dp"
  24. android:src="@drawable/action_bar_icon_search"
  25. android:layout_gravity="center"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"/>
  28. <ImageButton
  29. android:background="@null"
  30. android:layout_marginLeft="15dp"
  31. android:src="@drawable/action_bar_icon_add"
  32. android:layout_gravity="center"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"/>
  35. <ImageButton
  36. android:background="@null"
  37. android:layout_marginLeft="15dp"
  38. android:src="@drawable/action_bar_icon_more"
  39. android:layout_gravity="center"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"/>
  42. </LinearLayout>

2.对ActionBar属性进行设定

  1. actionBar.setDisplayHomeAsUpEnabled(false);
  2. actionBar.setDisplayShowTitleEnabled(false);
  3. actionBar.setDisplayShowCustomEnabled(true);
  4. actionBar.setDisplayUseLogoEnabled(false);
  5. actionBar.setHomeButtonEnabled(false);
  6. View view = getLayoutInflater().inflate(R.layout.main_actionbar,null);
  7. actionBar.setCustomView(view);

3.添加点击事件

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

  1. android:onClick="clickEvent"
  2. public void clickEvent(View view){
  3. if (view.getId() == R.id.btn){
  4. Intent intent = new Intent(MainActivity.this, UserActivity.class);
  5. startActivity(intent);
  6. }
  7. }

4.实现效果

Center

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读

    相关 android定义view

    最近弄的项目中在看到![Image 1][]这种![Center][]加减数量,如是就自己自定义了这样的view。 考虑到图标可能会被替换如是加了个attrs.xml文件,也

    相关 定义View笔记

    这里参考了洪洋大神博客中的许多内容,主要是用于学习自定义View的一个阶段性的总结。 使用自定义View的时候主要有以下的步骤: 1.定义这个View中需要使用到的属性。

    相关 Android定义View

    前几天在郭霖大神的博客上看了自定义View的知识,感觉受益良多,大神毕竟大神。在此总结一下关于Android 自定义View的用法: 首先,自定义View可以由基本控件或者组

    相关 Android定义View

    如何自定义控件 1. 自定义属性的声明和获取 2. 测量onMeasure:测量自定义控件的尺寸 3. 绘制onDraw:绘制自定义控件 4. 状态的存储与恢复: