简体   繁体   English

Android-使用TextView作为ToolBar标题时看不到ActionBar向上按钮

[英]Android - ActionBar Up button not visible when using TextView as ToolBar title

I have added a Toolbar on top of all of my activities. 我在所有活动的顶部都添加了Toolbar But I wanted the title to be in the centre, so as per some suggestions on SO, I created a TextView inside the Toolbar and I was able to put it in the centre. 但是我希望标题位于中间,因此根据SO的一些建议,我在Toolbar TextView创建了一个TextView ,然后将其放在了中心。
Now, when I am trying to add an Up Action following this guide Adding an Up Action | 现在,当我试图加入这个指南的后续行动 增加一个后续行动| Android Developers , I do not get any UpButton. Android开发人员 ,我没有得到任何UpButton。
Is this because of using a TextView inside of the Toolbar ? 这是因为在Toolbar使用了TextView吗? Because I read on SO that the Toolbar is basically a View and can be configured just like any other View , so I don't get why this should be a problem. 因为我在SO上读到,工具栏基本上是一个View并且可以像配置其他View一样进行配置,所以我不明白为什么这应该是一个问题。

  1. If that is the reason, and I'd have to use the default android:title="My Title" attribute in for the Toolbar` , is there any other way to put it in the centre ? 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 ,是否还有其他方法可以将其放在中间?
  2. What is the workaround for this problem so that I can get an Up Action as well as the toolbar title in the centre ? 解决此问题的方法是什么,以便我可以在中央进行向上操作以及工具栏标题?

Thanks for any help you'd be able to provide. 感谢您提供的任何帮助。

XML Layout 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 Code 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);
    }
}

You need to set the getSupportActionBar().setHomeButtonEnabled(true); 您需要设置getSupportActionBar()。setHomeButtonEnabled(true); enable the Up Action button 启用向上操作按钮

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM