使用setCustomView建立标题居中的自定义ActionBar
2014-09-16 10:39
686 查看
ActionBar因为溢出菜单的原因导致没有办法让标题居中,但是我们又经常有样式需要标题居中,所以使用自定义ActionBar的布局是最正确的选择,可以在xml布局文件中进行视图的定制。
定制使用到的api是ActionBar中提供的 void setCustomView(View) 函数。
定义的视图是这个样子的:
标题终于居中了。
这里没有涉及到任何ActionBar的style修改,也只有一个activity;
MainActivity.java
actionbar_layout.xml
b.png:
;k.png
虽然只有这两个文件用到和修改到,但是最小的版本也要支持ActionBar;
这里补充一下setHomeButtonEnabled函数,控制左上角图标是否显示出来,小于4.0版本的默认值为true的。在4.0及其以上默认值是false;
本文来自CSDN博客 转载请联系作者
并注明出处/article/2710319.html
定制使用到的api是ActionBar中提供的 void setCustomView(View) 函数。
定义的视图是这个样子的:
标题终于居中了。
这里没有涉及到任何ActionBar的style修改,也只有一个activity;
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 返回箭头(默认不显示) getActionBar().setDisplayHomeAsUpEnabled(false); // 左侧图标点击事件使能 getActionBar().setHomeButtonEnabled(true); // 使左上角图标(系统)是否显示 getActionBar().setDisplayShowHomeEnabled(false); // 显示标题 getActionBar().setDisplayShowTitleEnabled(false); //显示自定义视图 getActionBar().setDisplayShowCustomEnabled(true); View actionbarLayout = LayoutInflater.from(this).inflate( R.layout.actionbar_layout, null); getActionBar().setCustomView(actionbarLayout); } }
actionbar_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:paddingLeft="10dp" android:paddingRight="10dp" > <ImageButton android:id="@+id/left_imbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@null" android:src="@drawable/b" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#FFFFFFFF" android:textSize="18sp" android:text="标题" /> <!-- actionbar 右边按钮 --> <ImageButton android:id="@+id/right_imbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@null" android:src="@drawable/k" /> </RelativeLayout>补充两个用到的图标,透明白色图标(白色图标看不到,在空白处点击另存为就好)
b.png:
;k.png
虽然只有这两个文件用到和修改到,但是最小的版本也要支持ActionBar;
这里补充一下setHomeButtonEnabled函数,控制左上角图标是否显示出来,小于4.0版本的默认值为true的。在4.0及其以上默认值是false;
本文来自CSDN博客 转载请联系作者
并注明出处/article/2710319.html
相关文章推荐
- 使用setCustomView建立标题居中的自定义ActionBar
- ActionBar设置自定义setCustomView()留有空白的问题
- Android如何不用setCustomView而实现ActionBar的自定义修改
- Android使用setCustomTitle()方法自定义对话框标题
- Actionbar在viewpager中使用的标题效果
- Navigation Drawer 的初了解以及 ActionBar 自定义视图(标题居中)
- 关于android自定义View时使用paint.setXfermode注意
- ActionBar 使用自定义view 宽度填充不完全的解决方法
- Android ActionBar高级自定义——设置标题居中和添加控件
- [置顶] 当SearchView搜索的listView使用自定义适配器时,自定义setFilterText()方法(模糊查询筛选)
- android使用自定义标题时提示“you cannot combine custom titles with other  ”
- 自定义ViewGroup应如何去使用setMeasuredDimsion(int,int)和计算其参数
- android: spinner及setDropDownViewResource的使用及自定义Spinner样式
- Android ActionBar高级自定义——设置标题居中和添加控件
- 关于使用Visual Studio 2008 建立Setup And Deployment Project(安装与部署项目)制作安装.net 安装程序,插入Custom Actions(自定义动作)的办法
- Android Navigation Drawer,自定义ActionBar(标题居中)
- 在Visual Studio.NET中使用自定义插件最大化您的生产力(三)Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
- 在Visual Studio.NET中使用自定义插件最大化您的生产力(一)Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
- 在ASP.NET AJAX中使用自定义ViewManager局部更新数据
- 自定义View和Matrix的使用