【发布时间】:2011-03-27 04:20:15
【问题描述】:
目前它只显示应用程序的名称,我希望它显示自定义的内容,并且在我的应用程序中的每个屏幕上都不同。
例如:我的主屏幕可能在操作栏中显示“page1”,而应用切换到的另一个活动可能在该屏幕操作栏中显示“page2”。
【问题讨论】:
目前它只显示应用程序的名称,我希望它显示自定义的内容,并且在我的应用程序中的每个屏幕上都不同。
例如:我的主屏幕可能在操作栏中显示“page1”,而应用切换到的另一个活动可能在该屏幕操作栏中显示“page2”。
【问题讨论】:
下面的代码在 Oncreate 方法中对我很有效
getSupportActionBar().setTitle("Text to display");
【讨论】:
在 onCreateView 中添加:
(activity as AppCompatActivity).supportActionBar?.title ="My APP Title"
【讨论】:
如果您使用导航栏更改片段,那么您可以在您更改片段的地方添加更改,如下例所示:
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")));
将渐变设置为操作栏首先创建渐变: 示例 目录创建> R.drawable.gradient_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>
然后这样设置:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));
【讨论】:
科特林
您可以通过这种方式在 Kotlin 中以编程方式完成。 如果“supportActionBar”为空,你就忽略它:
supportActionBar?.setTitle(R.string.my_text)
supportActionBar?.title = "My text"
或者这样。 如果“supportActionBar”为空,它会抛出异常。
supportActionBar!!.setTitle(R.string.my_text)
supportActionBar!!.title = "My text"
【讨论】:
getSupportActionBar().setTitle("title");
【讨论】:
适用于使用 AndroidX 和 导航架构组件的未来开发者。
而不是使用上述解决方案之一设置工具栏标题,这可能非常痛苦,如果您想在返回堆栈更改时动态设置它,您可以在导航图中为片段的标题设置一个占位符,如下所示:
<fragment
android:id="@+id/some_fragment"
android:name="package.SomeFragment"
android:label="Hello {placeholder}"
tools:layout="@layout/fragment_some">
<argument
android:name="placeholder"
app:argType="string" />
</fragment>
必须使用FragmentDirections(通过操作方法)提供占位符值。
然后在标题中替换它并显示为Hello World(当placeholder = "World")。
【讨论】:
如果您在活动中,最简单的方法是致电this.setTitle("...")。
如果你在一个片段中,只需调用getActivity().setTitle("...");
这种方式可以让你随时更改标题,无需在setContentView(R.layout.activity_test)之前调用;
【讨论】:
您只需在onCreate 方法中添加此代码即可。
setTitle("new title");
【讨论】:
更改操作栏名称的最简单方法是转到 AndroidManifest.xml 并键入此代码。
<activity android:name=".MainActivity"
android:label="Your Label> </activity>
【讨论】:
更改操作栏名称的最佳方法是转到 AndroidManifest.xml 并输入此代码。
<activity android:name=".YourActivity"
android:label="NameYouWantToDisplay> </activity>
【讨论】:
ActionBar ab = getActionBar();
TextView tv = new TextView(getApplicationContext());
LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // Width of TextView
LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setTextColor(Color.RED);
ab.setCustomView(tv);
欲了解更多信息,请查看此链接:
http://android--code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html
【讨论】:
getActionBar().setTitle("edit your text");
【讨论】:
在 Activity.onCreate() 回调中或在其他需要更改标题的地方:
getSupportActionBar().setTitle("Whatever title");
【讨论】:
有点老,但有同样的问题。我是这样做的:
strings.xml
<string name="title_awesome_app">My Awesome App</string>
并确保在 AndroidManifest.xml 中进行设置:
<activity
...
android:label="@string/title_awesome_app" >
...
</activity>
这很简单,您不必担心空引用和其他问题。
【讨论】:
我们可以通过以下两种方式之一更改 ActionBar 标题:
在Manifest中:在manifest文件中,设置每个Activity的标签。
android:label="@string/TitleWhichYouWantToDisplay"
代码中:在代码中调用setTitle()方法,参数为String或String的id。
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);
}
}
【讨论】:
仅供参考,ActionBar 是在 API 级别 11 中引入的。ActionBar 是 Activity 顶部的一个窗口功能,可以显示 Activity 标题、导航模式和其他交互项目(如搜索)。
我完全记得自定义标题栏并使其在应用程序中保持一致。所以我可以和早期做一个比较,可以列出使用ActionBar的一些优点:
例如:
getActionBar().setTitle("Hello world App");
getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions
例如:
@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 为您提供基本和熟悉的外观、导航模式和其他要执行的快速操作。但这并不意味着它在每个应用程序中看起来都一样。您可以根据您的 UI 和设计要求对其进行自定义。您只需要定义和编写样式和主题。
如果您想为 ActionBar 生成样式,那么这个 Style Generator 工具可以帮助您。
================================================ ====================================
您可以通过设置Android:label
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
但如果您想以自己的方式自定义标题栏,即 Want to put Image icon and custom-text,那么以下代码适用于我:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/icon1"/>
<TextView
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
</LinearLayout>
public class TitleBar extends Activity {
@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);
}
}
}
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>
【讨论】:
您可以在Activity 中使用setTitle 以编程方式定义您的标题,此方法可以接受String 或values/strings.xml 文件中定义的ID。示例:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}
【讨论】:
尝试这样做...
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);
...
...
}
对我有用
【讨论】:
您可以在清单文件中为每个活动定义标签。
活动的正常定义如下所示:
<activity
android:name=".ui.myactivity"
android:label="@string/Title Text" />
标题文本应替换为此活动的字符串资源的 id。
如果你想动态设置,你也可以从代码中设置标题文本。
setTitle(address.getCity());
在我的活动的 oncreate 方法中,标题被设置为特定地址的城市。
【讨论】: