Android 如何更改操作栏上的文本
目前,它只显示应用程序的名称,我希望它显示一些自定义的内容,并在我的应用程序中的每个屏幕上有所不同Android 如何更改操作栏上的文本,android,android-actionbar,Android,Android Actionbar,目前,它只显示应用程序的名称,我希望它显示一些自定义的内容,并在我的应用程序中的每个屏幕上有所不同 例如:我的主屏幕可能在操作栏中显示“page1”,而应用程序切换到的另一个活动可能在该屏幕的操作栏中显示“page2”。您可以在清单文件中为每个活动定义标签 活动的正常定义如下所示: <activity android:name=".ui.myactivity" android:label="@string/Title Text" /> 通过此行,标题被设置为“我
例如:我的主屏幕可能在操作栏中显示“page1”,而应用程序切换到的另一个活动可能在该屏幕的操作栏中显示“page2”。您可以在清单文件中为每个活动定义标签 活动的正常定义如下所示:
<activity
android:name=".ui.myactivity"
android:label="@string/Title Text" />
通过此行,标题被设置为“我的活动”的oncreate方法中特定地址的城市。更新:最新的ActionBar(标题)模式:
供参考,已在API 11级中引入。ActionBar是活动顶部的一个窗口功能,可以显示活动标题、导航模式和其他交互项目,如搜索
我完全记得定制标题栏并使其在应用程序中保持一致。因此,我可以与前几天进行比较,并列出使用ActionBar的一些优点:
@Override
public void setActionBar(String heading) {
// TODO Auto-generated method stub
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
actionBar.setTitle(heading);
actionBar.show();
}
=>正常方式,
=>自定义操作栏,
例如:
@Override
public void setActionBar(String heading) {
// TODO Auto-generated method stub
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
actionBar.setTitle(heading);
actionBar.show();
}
设置操作栏的样式:
ActionBar为您提供基本和熟悉的外观、导航模式和其他要执行的快速操作。但这并不意味着它在每个应用程序中看起来都一样。您可以根据用户界面和设计要求对其进行自定义。你只需要定义和写风格和主题
更多信息请访问:
如果您想为ActionBar生成样式,那么这个工具可以帮助您
=================================================================================
旧:早期:
=>正常方式,
通过设置每个屏幕的Android:label
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
titlebar.xml
strings.xml
strings.xml文件在values
文件夹下定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Set_Text_TitleBar!</string>
<string name="app_name">Set_Text_TitleBar</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
你好,世界,设置标题栏!
设置标题栏
#3232CD
#FFFF00
尝试这样做
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setTitle(String.format(your_format_string, your_personal_text_to_display));
setContentView(R.layout.your_layout);
...
...
}
它适用于我您可以在
活动
中使用设置标题
以编程方式定义标题,此方法可以接受字符串
或值/strings.xml
文件中定义的ID。例如:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}
我们可以通过以下两种方式之一更改ActionBar标题:
android:label="@string/TitleWhichYouWantToDisplay"
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleWhichYouWantToDisplay);
// OR You can also use the line below
// setTitle("MyTitle")
setContentView(R.layout.activity_main);
}
}
年纪大了一点,但也有同样的问题。我是这样做的:
<activity
android:name=".ui.myactivity"
android:label="@string/Title Text" />
strings.xml
我最棒的应用程序
并确保在您的AndroidManifest.xml中设置此项:
<activity
...
android:label="@string/title_awesome_app" >
...
</activity>
...
这很简单,您不必担心空引用和其他内容。在Activity.onCreate()回调中或在需要更改标题的其他位置:
getSupportActionBar().setTitle("Whatever title");
有关更多信息,请查看此链接:
如果您在活动中,最简单的方法是调用
this.setTitle(“…”
。
如果您在一个片段中,只需调用getActivity().setTitle(“…”)
通过这种方式,您可以随时更改标题,无需在
setContentView(R.layout.activity_test)
之前调用它 更改操作栏名称的最佳方法是转到AndroidManifest.xml
然后键入此代码
<activity android:name=".YourActivity"
android:label="NameYouWantToDisplay> </activity>
<activity android:name=".MainActivity"
android:label="Your Label> </activity>
更改操作栏名称的最简单方法是转到AndroidManifest.xml并键入此代码
<activity android:name=".YourActivity"
android:label="NameYouWantToDisplay> </activity>
<activity android:name=".MainActivity"
android:label="Your Label> </activity>
您只需在onCreate
方法中添加此代码
setTitle("new title");
适用于使用AndroidX和导航体系结构组件的未来开发人员
而不是使用上述解决方案之一设置工具栏标题,这可能是非常痛苦的如果要在后堆栈更改时动态设置,可以为导航图中片段的标题设置一个占位符,如下所示:
占位符值必须使用FragmentDirections
(通过操作方法)提供
然后在标题中替换它,并显示为Hello World
(当placeholder=“World”
时)。getSupportActionBar().setTitle(“标题”)代码>Kotlin
通过这种方式,您可以在Kotlin中以编程方式完成它。如果“supportActionBar”为空,则忽略它:
或者这样。如果“supportActionBar”为null,则抛出异常
如果您使用导航栏更改片段,那么您可以在更改片段的位置添加更改,如以下示例所示:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.clientsidedrawer:
// Toast.makeText(getApplicationContext(),"Client selected",Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new clients_fragment()).commit();
break;
case R.id.adddatasidedrawer:
getSupportActionBar().setTitle("Add Client");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addclient_fragment()).commit();
break;
case R.id.editid:
getSupportActionBar().setTitle("Edit Clients");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Editclient()).commit();
break;
case R.id.taskid:
getSupportActionBar().setTitle("Task manager");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Taskmanager()).commit();
break;
如果您使用简单活动,则只需呼叫:
getSupportActionBar().setTitle("Contact Us");
要在活动中更改操作栏/工具栏颜色,请使用:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#06023b")));
要将渐变设置为actionbar首先创建渐变:
示例直接创建>R.drawable.gradient\u contactus
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="90"
android:startColor="#2980b9"
android:centerColor="#6dd5fa"
android:endColor="#2980b9">
</gradient>
</shape>
在onCreateView中添加以下内容:
(activity as AppCompatActivity).supportActionBar?.title ="My APP Title"
按照此示例在android中为活动或整个应用程序全局设置自定义标题:+1!谢谢,但是你能告诉我为什么android:layout\u width=“400px”会出现在父线性布局中吗?@quinestor是的,如果你愿意,你可以使用“wrap\u content”或“fill\u parent”,这只是个例子。一些javascript提示?我反对使用“px”而不是“dp”这样的独立单元因为很多新的学习者学到的不仅仅是通过尝试分解代码和获得答案来回答的问题
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.clientsidedrawer:
// Toast.makeText(getApplicationContext(),"Client selected",Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new clients_fragment()).commit();
break;
case R.id.adddatasidedrawer:
getSupportActionBar().setTitle("Add Client");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addclient_fragment()).commit();
break;
case R.id.editid:
getSupportActionBar().setTitle("Edit Clients");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Editclient()).commit();
break;
case R.id.taskid:
getSupportActionBar().setTitle("Task manager");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Taskmanager()).commit();
break;
getSupportActionBar().setTitle("Contact Us");
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#06023b")));
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="90"
android:startColor="#2980b9"
android:centerColor="#6dd5fa"
android:endColor="#2980b9">
</gradient>
</shape>
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));
(activity as AppCompatActivity).supportActionBar?.title ="My APP Title"