[英]How to change action bar Title
I have developed a action-bar in Android Studio, however, the action-bar displays the activity name rather than the app name along with the icons. 我已经在Android Studio中开发了一个动作栏,但是,该动作栏显示活动名称而不是应用程序名称以及图标。
For example I want it to display the app-name plus the refresh, setting and User Icon. 例如,我希望它显示应用程序名称以及刷新,设置和用户图标。 Instead it display the activity name along with refresh, setting and User Icon.
而是显示活动名称以及刷新,设置和用户图标。
Menuactivity.java Menuactivity.java
public class MenuActivity extends ActionBarActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_user) {
Intent intent= new Intent(this,DashboardActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MenuMain shows the application name however once run the code it displays the activity name rather than the app name, how can I rectify this?. MenuMain显示应用程序名称,但是一旦运行代码,它就会显示活动名称而不是应用程序名称,我该如何纠正?
1. The easiest way is: 1.最简单的方法是:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.strind.app_name);
2. You can also use a custom Toolbar
layout 2.您也可以使用自定义
Toolbar
布局
Change your existing code us 更改我们现有的代码
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
To like this: 要这样:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_main;
Follow please these my tutorial to see how to implement it. 请遵循这些我的教程,以了解如何实现它。
Here's my solution - create a TextDrawable logo using custom toolbar这是我的解决方案-使用自定义工具栏创建TextDrawable徽标
- Create a custom layout with name
action_bar.xml
创建名称为
action_bar.xml
的自定义布局Put into it this code
将此代码放入其中
<ImageView android:id="@+id/image_view" android:layout_width="32dp" android:layout_height="32dp" tools:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="@string/app_name"> //THIS GUY android:textColor="#ffffff" android:textSize="24sp" android:textAlignment="gravity" android:gravity="center_vertical"/>
Add to your
onCreate
method inMainActivity
class this code:将以下代码添加到
MainActivity
类的onCreate
方法中://SET A DRAWABLE TO IMAGEVIEW Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.actionbar_main); TextDrawable drawable = TextDrawable.builder() .buildRound("A", getResources().getColor(R.color.colorAccent)); ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setImageDrawable(drawable);
After changes it should look like:
更改后,它应类似于:
NOTICE: To show appName i used TextView
with android:text="@string/app_name"
value. 注意:为了显示appName,我将
TextView
与android:text="@string/app_name"
值一起使用。
Hope it help 希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.