我正在try 使用android-support-v7:21库创建简单的应用程序

public class MainActivity extends ActionBarActivity {

    Toolbar mActionBarToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
        mActionBarToolbar.setTitle("My title");
        setSupportActionBar(mActionBarToolbar);
}

主要活动.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar            
        android:id="@+id/toolbar_actionbar"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:fitsSystemWindows="true" />

</LinearLayout>

但不是工具栏上的"我的标题",而是显示%application name%

UPD: 在此之前,styles.xml是:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
</style>

所以,我认为actionbar没有被使用.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

但问题并没有得到解决.

推荐答案

找到解决方案:

而不是:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);

我用过:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

而且很管用.

Android相关问答推荐

ArrayList.remove()(Kotlin中的removeAt())导致奇怪的IndexOutOfBoundsResponse异常

懒惰的垂直网格中盒子的重量-Jetpack组合

fillMaxHeight中的分数在列内不起作用(android jetpack compose)

在卡片上创建圆角底部边框

用于小部件泄漏警告的伙伴对象中的Kotlin Lateinit

如何在Jetpack Compose中更新异步回调的用户界面

无法加载类';com.android.build.api.extension.AndroidComponentsExtension';

如何在DownloadManager Android中显示ProgressBar和Complete Listener

触发PurchasesUpdatedListener回调时,billingClient.launchBillingFlow之前设置的成员变量丢失

我无法连接到信号机

Dispatchers中的Kotlin协同程序.Main没有';t块主螺纹

需要 java 17 而不是 java 11:Android CI-CD GitHub Actions

如何在Android中使用嵌套的Recyclerview

Android Studio电鳗:javaHome好像无效

如何正确地将图像上传到 Jetpack Compose 中的 LazyList 中的项目?

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

Android Jetpack Compose - 找不到 R.drawable?

compose FontFamily 错误:必须初始化变量

在 android-billing-5.0 中获取 ProductDetails 价格

在 compose 屏幕之间传递 uri 会导致:SecurityException: Permission Denial