在 android 应用中 Toolbar.setTitle 方法无效 - 应用名称显示为标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26486730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In android app Toolbar.setTitle method has no effect – application name is shown as title
提问by krossovochkin
I'm trying to create simple application using android-support-v7:21 library.
Code snippets:
MainActivity.java
我正在尝试使用 android-support-v7:21 库创建简单的应用程序。
代码片段:
MainActivity.java
public class MainActivity extends ActionBarActivity {
Toolbar mActionBarToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);
}
activity_main.xml
活动_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_actionbar"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:fitsSystemWindows="true" />
</LinearLayout>
But instead of "My title" on Toolbar %application name% is shown.
Seems like setTitle
method has no effect.
I would like to show "My title".
但显示的是工具栏 %application name% 上的“我的标题”。
好像setTitle
方法没有效果。
我想显示“我的标题”。
UPD:Before, styles.xml
was:
UPD:之前,styles.xml
是:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
</style>
So, I thought that actionbar is not used.
I add NoActionBar
to style parent:
所以,我认为没有使用 actionbar。我添加NoActionBar
到样式父项:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
</style>
But the problem is not resolved.
但是问题没有解决。
回答by krossovochkin
Found the solution:
找到了解决办法:
Instead of:
代替:
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);
I used:
我用了:
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");
And it works.
它有效。
回答by sorianiv
For anyone who needs to set up the title through the Toolbarsome time after setting the SupportActionBar, read this.
对于需要在设置 SupportActionBar 一段时间后通过工具栏设置标题的任何人,请阅读此内容。
The internal implementation of the support library just checks if the Toolbar has a title (not null) at the moment the SupportActionBar is set up. If there is, then this title will be used instead of the window title. You can then set a dummy title while you load the real title.
支持库的内部实现只是在设置 SupportActionBar 时检查 Toolbar 是否有标题(非空)。如果有,则将使用此标题而不是窗口标题。然后,您可以在加载真实标题时设置虚拟标题。
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("");
setSupportActionBar(mActionBarToolbar);
later...
之后...
mActionBarToolbar.setTitle(title);
回答by Pratik Butani
The above answer is totally true but not working for me.
上面的答案是完全正确的,但对我不起作用。
I solved my problem with the following things.
我通过以下事情解决了我的问题。
Actually My XML is like that:
其实我的 XML 是这样的:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/confirm_order_mail_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/confirm_order_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/confirm_order_list_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.Toolbar
android:id="@+id/confirm_order_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
I have tried all the option and after all I just removed CollapsingToolbarLayout
because of i do not need to use in that particular XML
So My Final XMLis like:
我已经尝试了所有选项,毕竟我只是删除了CollapsingToolbarLayout
因为我不需要在那个特定的地方使用XML
所以我的最终 XML就像:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/confirm_order_mail_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/confirm_order_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/confirm_order_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Now you have to use setTitle()
like above answer:
现在你必须使用setTitle()
上面的答案:
mActionBarToolbar = (Toolbar) findViewById(R.id.confirm_order_toolbar_layout);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My Title");
Now If you want to use CollapsingToolbarLayout
and Toolbar
together then you have to use setTitle()
of CollapsingToolbarLayout
现在,如果你想使用CollapsingToolbarLayout
和Toolbar
再在一起,你必须使用setTitle()
的CollapsingToolbarLayout
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.confirm_order_mail_layout);
collapsingToolbarLayout.setTitle("My Title");
May it will helps you. Thank you.
愿它会帮助你。谢谢你。
回答by roshan posakya
Simply you can change any acitivity name by using
只需使用以下命令即可更改任何活动名称
Acitivityname.this.setTitle("Title NAme");
回答by Axrorxo'ja Yodgorov
Try this, you can define title directly in XML:
试试这个,你可以直接在 XML 中定义标题:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="some title"
app:popupTheme="@style/AppTheme.PopupOverlay">
回答by roshan posakya
To set the title for each Navbar fragment title
为每个导航栏片段标题设置标题
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
myView = inflater.inflate(R.layout.second_layout, container, false);
getActivity().setTitle("title");
return myView;
}
回答by Ragu Swaminathan
Try this .. this method works for me..!! hope it may help somebody..!!
试试这个..这个方法对我有用..!!希望它可以帮助某人.. !!
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:text="Toolbar Title"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
To display logo in toolbar try the below snippet. // Set drawable
要在工具栏中显示徽标,请尝试以下代码段。// 设置可绘制
toolbar.setLogo(ContextCompat.getDrawable(context, R.drawable.logo));
Let me know the result.
让我知道结果。
回答by Hanisha
getSupportActionBar().setTitle("Your Title");
回答by redDragonzz
Please see https://code.google.com/p/android/issues/detail?id=77763. Apparently it is supposed to work like that. Once you call the setSupportActionBar()
method call, it then is the responsibility of the ActionBar delegate to route the call to the correct view.
请参阅https://code.google.com/p/android/issues/detail?id=77763。显然它应该像那样工作。一旦调用了setSupportActionBar()
方法调用,ActionBar 委托就负责将调用路由到正确的视图。
回答by Nikita G.
I tried to rename the toolbar from the fragment
我试图从片段重命名工具栏
It helped me, I hope to help someone else
它帮助了我,我希望能帮助别人
Activity activity = this.getActivity();
Toolbar toolbar = (Toolbar) activity.findViewById(R.id.detail_toolbar);
if (toolbar != null) {
activity.setTitle("Title");
}
//toolbar.setTitle("Title"); did not give the same results