使用setCustomView建立标题居中的自定义ActionBar
来源:互联网 发布:怎么看淘宝收货时间 编辑:程序博客网 时间:2024/05/19 20:19
ActionBar因为溢出菜单的原因导致没有办法让标题居中,但是我们又经常有样式需要标题居中,所以使用自定义ActionBar的布局是最正确的选择,可以在xml布局文件中进行视图的定制。
定制使用到的api是ActionBar中提供的 void setCustomView(View) 函数。
定义的视图是这个样子的:
标题终于居中了。
这里没有涉及到任何ActionBar的style修改,也只有一个activity;
MainActivity.java
public class MainActivity extends Activity {@Overrideprotected 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博客 转载请联系作者
并注明出处http://blog.csdn.net/dreamintheworld/article/details/39314121
0 0
- 使用setCustomView建立标题居中的自定义ActionBar
- 使用setCustomView建立标题居中的自定义ActionBar
- 自定义ActionBar,ActionBar标题居中
- ActionBar自定义视图(标题居中)
- Android如何不用setCustomView而实现ActionBar的自定义修改
- ActionBar设置自定义setCustomView()留有空白的问题
- actionbar居中的view,标题
- actionbar的样式修改与标题居中
- ActionBar标题居中
- ActionBar标题居中
- 自定义的ActionBar实现标题的统一
- 如何设置ActionBar标题居中
- ActionBar自定义标题
- Android ActionBar高级自定义——设置标题居中和添加控件
- Android ActionBar高级自定义——设置标题居中和添加控件
- ActionBar 添加自定义控件居中
- ActionBar “标题”居中并添加“返回”
- 自定义ActionBar的高度、标题与菜单中的文字样式
- 从JAR文件中读取文件
- PHP版本的区别
- C++ primer 第五版 中文版 练习 10.34 个人code
- JAVA_形象生动的单例模式总结
- 斯诺登泄露文件:英美联手监听德国电信
- 使用setCustomView建立标题居中的自定义ActionBar
- Linker error
- C/C++ 使用宏在编译期计算可变参数个数
- Get the 48×48 or 256×256 icon of a file on Windows
- Cocos2d-x3.2 iOS下开启多点触控
- CentOS 打包压缩文件 zip 命令详解
- android平板上EditText总是自动弹出输入法问题
- webview setting movie path:程序崩溃
- 数据结构之线性表4单链表(静态单链表)