[英]Android - ActionBar Up button not visible when using TextView as ToolBar title
我在所有活动的顶部都添加了Toolbar
。 但是我希望标题位于中间,因此根据SO的一些建议,我在Toolbar
TextView
创建了一个TextView
,然后将其放在了中心。
现在,当我试图加入这个指南的后续行动 增加一个后续行动| Android开发人员 ,我没有得到任何UpButton。
这是因为在Toolbar
使用了TextView
吗? 因为我在SO上读到,工具栏基本上是一个View
并且可以像配置其他View
一样进行配置,所以我不明白为什么这应该是一个问题。
If that is the reason, and I'd have to use the default
栏中If that is the reason, and I'd have to use the default
android:title =“ My Title” attribute in for the
,是否还有其他方法可以将其放在中间? 感谢您提供的任何帮助。
XML布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/lifeline_toolbar"
android:elevation="4dp"
android:background="@android:color/background_dark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_gravity="center_horizontal"
android:text="Lifeline"
android:textStyle="bold"
android:textSize="18sp"
android:id="@+id/lifeline_toolbar_title"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<LinearLayout>
</LinearLayout>
<LinearLayout>
</LinearLayout>
</LinearLayout>
Java代码
public class ll_home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ll_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
您需要设置getSupportActionBar()。setHomeButtonEnabled(true); 启用向上操作按钮
Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.