在android中更改工具栏标题

时间:2018-05-08 08:12:10

标签: java android android-studio

我想在工具栏中使用自定义文本而不是App Name 我在xml

中使用此功能
 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="20dp">

        <TextView
            android:id="@+id/toolbartitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="my toolbar"/>

    </android.support.v7.widget.Toolbar>

并使用此

 Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
    TextView mytext=findViewById(R.id.toolbartitle);
    setSupportActionBar(toolbar);
MainActivity中的

但它显示像这样

MyApp的

enter image description here

我如何纠正它才能显示我的文字?

3 个答案:

答案 0 :(得分:1)

试试这个,

Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
TextView mytext=findViewById(R.id.toolbartitle);
setSupportActionBar(toolbar);
getSupprotActionBar().setTitle(" ");  // impotent

更新:

或者您可以在清单文件

中执行此操作
<activity
    android:name=".SelectionActivity"
    android:label="" <!-- impotent -->
    android:screenOrientation="portrait" />

快乐编码。

答案 1 :(得分:1)

试试这个

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setTitle("");
        toolbar.setSubtitle("");

setDisplayShowTitleEnabled可帮助您从工具栏中停用标题。

答案 2 :(得分:0)

删除TextView内的Toolbar,您不需要。{/ p>

将此行添加到Toolbar定义

app:title="@string/app_name"