700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android actionbar 文字居中 ActionBar设置自定义布局使文字居中

android actionbar 文字居中 ActionBar设置自定义布局使文字居中

时间:2019-09-20 19:19:38

相关推荐

android actionbar 文字居中 ActionBar设置自定义布局使文字居中

在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();

}

});

下面是布局界面:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fadingEdge="none"

android:orientation="horizontal" >

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" />

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" />

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" />

ok.问题解决

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。