我希望在我的 Android 应用程序中有一个像我在 iOS 应用程序中那样的操作栏:
.
不幸的是,我不知道如何制作仅包含文本的后退按钮以及如何将标题移动到中心。这将适用于整个应用程序,而不仅仅是一种布局。
有人可以帮我吗?
我找到了一个简单的解决方案。
您只需为自定义 ActionBar 创建布局custom_action_bar_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="50dp"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/back_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff"
android:text="Back"/>
</RelativeLayout>
然后在你的ExampleActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_date);
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_action_bar_layout, null);
TextView title = (TextView) mCustomView.findViewById(R.id.title_text);
title.setText("Title");
TextView backButton = (TextView) mCustomView.findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
这就是结果。 https://i.stack.imgur.com/eTVg9.png
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)