我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
当前回答
你也可以这样做:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
其他回答
我在onNavigationItemSelected内部使用了以下调用:
HomeActivity.this.setTitle(item.getTitle());
为此,可以采用两种方式:XML或Java。看这里:如何更改操作栏上的文本
So:
XML:
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
Java:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
你也可以这样做:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
在活动中的onCreate函数中添加以下代码。
setTitle("NewName");
我得到了非静态方法setTitle(CharSequence)不能从静态上下文错误中引用,因为我在静态PlaceholderFragment类中使用了setTitle()。我通过使用getActivity(). getactionbar()来解决它。setTitle(“新标题”);