ActionBar设置自定义布局使文字居中
2016-03-12 17:13
274 查看
在ToolBar出现之前,安卓顶部使用的是ActionBar技术,但是系统自带的ActionBar有几个问题,一是系统自带的返回键是一个小于号,Title文字的位置也默认在左边.如果有产品要求我们做出下图的界面效果,就要开动大脑了.
解决方案为:在ActionBar中添加自己的xml布局
代码如下:
ActionBar mActionbar = getActionBar();
if (mActionbar == null) {
return;
}
mActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionbar.setDisplayShowCustomEnabled(true);
mActionbar.setCustomView(R.layout.action_bar_top_base);
tvTitle = (TextView) mActionbar.getCustomView().findViewById(
R.id.tv_title);
mActionbar.getCustomView().findViewById(R.id.iv_back)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
下面是布局界面:
<?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:fadingEdge="none"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/ic_action_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="几何社区"
android:textColor="@color/Black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:visibility="gone"
android:text=""
android:textColor="@color/Black"
android:textSize="14sp" />
</RelativeLayout>
ok.问题解决
解决方案为:在ActionBar中添加自己的xml布局
代码如下:
ActionBar mActionbar = getActionBar();
if (mActionbar == null) {
return;
}
mActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionbar.setDisplayShowCustomEnabled(true);
mActionbar.setCustomView(R.layout.action_bar_top_base);
tvTitle = (TextView) mActionbar.getCustomView().findViewById(
R.id.tv_title);
mActionbar.getCustomView().findViewById(R.id.iv_back)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
下面是布局界面:
<?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:fadingEdge="none"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/ic_action_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="几何社区"
android:textColor="@color/Black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:visibility="gone"
android:text=""
android:textColor="@color/Black"
android:textSize="14sp" />
</RelativeLayout>
ok.问题解决
相关文章推荐
- Poj 3050 Hopscotch【dfs】
- CentOS6.5 yum安装配置nginx 以及相关配置
- 选择Session还是Cookie
- NVARCHAR 和VARCHAR区别和使用
- Git的基本概念
- java concurrent
- Sum It Up
- UILabel补充
- ping某个域名的详细过程
- 能率热水器存在严重质量隐患!千万不要购买!买了之后每年都需要自费维修!
- 通完古墓丽影9
- 基于稀疏表示的人脸识别
- MBR与GPT的分区结构
- FancyBox的ajax利用json形成gallery
- Zookeeper 安装和部署
- ActiveMQ 即时通讯服务 浅析
- Unity面试着重复习重点
- ButterKnife的简单使用
- ListView 和 Adapter用法
- 杨辉三角形