应用程序运行时,活动标题不会显示

时间:2018-04-23 05:27:03

标签: android android-studio kotlin

我在工具栏中有一个带有自己标题的活动。但是,当应用程序运行时,标题不会显示。

这是Android Studio中的活动:

当应用程序在真实手机或模拟器上运行时,这就是它的样子:

enter image description here

标题的字符串存储在strings.xml

<resources>
<string name="app_name">Glag</string>
<string name="selection">Seleccione el algoritmo:</string>
<string name="dda">DDA</string>
<string name="bresenham">Bresenham</string>
<string name="dataInput">Inserte los puntos a evaluar:</string>
<string name="x1">X1:</string>
<string name="y1">Y1:</string>
<string name="x2">X2:</string>
<string name="y2">Y2:</string>
<string name="button">Calcular</string>
<string name="hint1">0</string>
<string name="hint2">10</string>
<string name="title_activity_dda">DDA Algorithm</string>
<string name="title_activity_bresenham">Bresenham Algorithm</string>
<string name="x">X</string>
<string name="y">Y</string>
<string name="p">P</string>

这是我的styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

3 个答案:

答案 0 :(得分:3)

Activity onCreate()

中添加以下代码
Toolbar toolbar = findViewById(R.id.toolbar); // your toolbar id. this is needed to set your custom toolbar or externally added toolbar as the actionbar toolbar. or you have to use Theme as Dark or Light ActionBar Theme insted of NoActionBar Theme. 
getSupportActionBar().setDisplayShowTitleEnabled(true); // sometime this may be set false causes no Title on ActionBar
setSupportActionBar(toolbar);

您可以使用 getSupportActionBar().setTitle("You Title"); Actionbar上设置自定义标题

希望这会对你有所帮助

答案 1 :(得分:3)

在活动类的onCreate()方法中尝试:

Toolbar appToolbar;
appToolbar = findViewById(R.id.include_toolbar);
setSupportActionBar(appToolbar);
setTitle("Activity Name");

答案 2 :(得分:1)

如果您不想要操作栏,请将其设置为style.xml中的noactionbar并在您的活动中尝试此操作

Toolbar t = findViewById(R.id.toolbar);
    setSupportActionBar(t);
    getSupportActionBar().setTitle("My Title");