【问题标题】:Why is app name not showing in Toolbar in android?为什么应用程序名称没有显示在 android 的工具栏中?
【发布时间】:2017-06-12 08:49:34
【问题描述】:

应用名称未显示在应用栏中。它的白色。我不知道为什么。请帮忙。之前有显示。然后我做了很多改变现在无法修复它

在主要活动中

....
<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:visibility="visible"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


  ....

</RelativeLayout>

风格

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

在安卓清单中

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    >
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

public class MainActivity extends AppCompatActivity  {

    public static final String LOG_TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the view pager that will allow the user to swipe between fragments
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // Create an adapter that knows which fragment should be shown on each page
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        // Set the adapter onto the view pager
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        // Connect the tab layout with the view pager. This will
        //   1. Update the tab layout when the view pager is swiped
        //   2. Update the view pager when a tab is selected
        //   3. Set the tab layout's tab names with the view pager's adapter's titles
        //      by calling onPageTitle()
        tabLayout.setupWithViewPager(viewPager);

    }
}

【问题讨论】:

  • 你是否在 res>values>strings 中指定了应用名称
  • 您需要将myToolbar初始化和setSupportActionBar()调用移到setContentView()之后。

标签: java android


【解决方案1】:

要么将它添加到你的 java 文件中。它将true 设置为显示标题栏属性,使其可见。

getSupportActionBar().setDisplayShowTitleEnabled(true);

或者在你的xml工具栏元素下添加这个

app:title="@string/app_name"

app 用于表示支持库小部件中包含的属性。

最重要的是,您的string.xml 文件中应该有app_name 字符串。

标题居中

您必须为操作栏设计自己的自定义布局并将其设置为。 How to set title in centre in ActionBar?

【讨论】:

  • 谢谢。你知道如何让文字居中对齐吗?
【解决方案2】:

你可以这样做..

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.setTitle("Your App Name");

【讨论】:

    【解决方案3】:

    风格

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

    这是你的问题。您输入 NoActionBar ,这意味着它不会在顶部有栏。

    您应该将DarkActionBar 设置为在进行更改之前拥有默认的ActionBar。

    希望对你有帮助!

    【讨论】:

    猜你喜欢
    • 2017-05-10
    • 2015-09-29
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多