方向更改时工具栏 (SupportActionBar) 标题更改为应用程序名称
Toolbar (SupportActionBar) title changes to app name on orientation change
我的 Activity:
中有此代码
protected void onCreate(Bundle savedInstanceState) {
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
...
}
我正在从 onResume()
中的各种片段更新 ActionBar 标题:
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
这工作正常,但方向改变后,标题再次变为应用程序名称。我该如何克服这个问题?
编辑:
经过更多调查后,我尝试了这个并发现了这种奇怪的行为:
在 Fragments 中设置标题的地方添加了这段代码:
final ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
if (actionBar != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("SWAPNIL", "IN RUN BEFORE: " + actionBar.getTitle());
actionBar.setTitle(title);
Log.d("SWAPNIL", "IN RUN AFTER : " + actionBar.getTitle());
}
}, 3000);
}
这是日志:
10-13 10:27:04.526 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: MY APP NAME
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
10-13 10:27:21.012 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: title
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
它正在根据日志进行更改,但未反映在 UI 中。
请帮帮我!
当您旋转屏幕时,您 activity 正在重新创建。所以 onCreate 再次被调用,而你还没有在那里设置标题。
将以下内容添加到您的 onCreate 中:
actionBar.setTile("Enter title here")
在清单文件中添加
<activity
android:name=".Activity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
或
onConfigChange()
中的代码
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
// Set Title for Landscape
}else{
// Set Title for Portrait
好吧,终于,在这个愚蠢的事情上花了 2 天之后,我得到了解决方案(我会说解决方法)。
这可能是一个嵌套的 Fragment
错误。
我有嵌套的片段结构。众所周知Fragment.getActivity()
returnsparentActivity
。经过大量调试后,我观察到如果你在方向改变后调用 getActivity()
(即使在 Fragment.onActivityCreated()
内部),它 returns 旧 Activity
的引用,除了最上面的 parent 片段正确 returns 新创建的 Activity
.
所以我写了这个方法来从任何 Fragment
:
获取当前 Activity
/**
* When inside a nested fragment and Activity gets recreated due to reasons like orientation
* change, {@link android.support.v4.app.Fragment#getActivity()} returns old Activity but the top
* level parent fragment's {@link android.support.v4.app.Fragment#getActivity()} returns current,
* recreated Activity. Hence use this method in nested fragments instead of
* android.support.v4.app.Fragment#getActivity()
*
* @param fragment
* The current nested Fragment
*
* @return current Activity that fragment is hosted in
*/
public Activity getActivity(Fragment fragment) {
if (fragment == null) {
return null;
}
while (fragment.getParentFragment() != null) {
fragment = fragment.getParentFragment();
}
return fragment.getActivity();
}
也许您可以在 Activity 的 onPostCreate() 方法中更改操作栏/工具栏。
代码很简单。我多次尝试更改我的模拟器/设备方向,它的效果很好..
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
toolbar.setTitle(mTitle);
}
你需要在这里看到sorianiv写的答案:
In android app Toolbar.setTitle method has no effect – application name is shown as title
添加额外的 toolbar.setTitle("")
为我解决了这个问题。
Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
toolbar.setTitle("");
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("Profiles");
我刚 运行 遇到了同样的问题,我用 getSupportActionBar
而不是 toolbar
解决了它。
getSupportActionBar().setTitle("...");
我的 Activity:
中有此代码protected void onCreate(Bundle savedInstanceState) {
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
...
}
我正在从 onResume()
中的各种片段更新 ActionBar 标题:
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
这工作正常,但方向改变后,标题再次变为应用程序名称。我该如何克服这个问题?
编辑: 经过更多调查后,我尝试了这个并发现了这种奇怪的行为: 在 Fragments 中设置标题的地方添加了这段代码:
final ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
if (actionBar != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("SWAPNIL", "IN RUN BEFORE: " + actionBar.getTitle());
actionBar.setTitle(title);
Log.d("SWAPNIL", "IN RUN AFTER : " + actionBar.getTitle());
}
}, 3000);
}
这是日志:
10-13 10:27:04.526 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: MY APP NAME
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
10-13 10:27:21.012 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: title
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
它正在根据日志进行更改,但未反映在 UI 中。 请帮帮我!
当您旋转屏幕时,您 activity 正在重新创建。所以 onCreate 再次被调用,而你还没有在那里设置标题。
将以下内容添加到您的 onCreate 中:
actionBar.setTile("Enter title here")
在清单文件中添加
<activity
android:name=".Activity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
或 onConfigChange()
中的代码if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
// Set Title for Landscape
}else{
// Set Title for Portrait
好吧,终于,在这个愚蠢的事情上花了 2 天之后,我得到了解决方案(我会说解决方法)。
这可能是一个嵌套的 Fragment
错误。
我有嵌套的片段结构。众所周知Fragment.getActivity()
returnsparentActivity
。经过大量调试后,我观察到如果你在方向改变后调用 getActivity()
(即使在 Fragment.onActivityCreated()
内部),它 returns 旧 Activity
的引用,除了最上面的 parent 片段正确 returns 新创建的 Activity
.
所以我写了这个方法来从任何 Fragment
:
Activity
/**
* When inside a nested fragment and Activity gets recreated due to reasons like orientation
* change, {@link android.support.v4.app.Fragment#getActivity()} returns old Activity but the top
* level parent fragment's {@link android.support.v4.app.Fragment#getActivity()} returns current,
* recreated Activity. Hence use this method in nested fragments instead of
* android.support.v4.app.Fragment#getActivity()
*
* @param fragment
* The current nested Fragment
*
* @return current Activity that fragment is hosted in
*/
public Activity getActivity(Fragment fragment) {
if (fragment == null) {
return null;
}
while (fragment.getParentFragment() != null) {
fragment = fragment.getParentFragment();
}
return fragment.getActivity();
}
也许您可以在 Activity 的 onPostCreate() 方法中更改操作栏/工具栏。 代码很简单。我多次尝试更改我的模拟器/设备方向,它的效果很好..
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
toolbar.setTitle(mTitle);
}
你需要在这里看到sorianiv写的答案: In android app Toolbar.setTitle method has no effect – application name is shown as title
添加额外的 toolbar.setTitle("")
为我解决了这个问题。
Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
toolbar.setTitle("");
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("Profiles");
我刚 运行 遇到了同样的问题,我用 getSupportActionBar
而不是 toolbar
解决了它。
getSupportActionBar().setTitle("...");