简体   繁体   English

工具栏将不会显示在“活动”中

[英]Toolbar won't display in Activity

I've set my application theme to be NoActionBar in AndroidManifest.xml because I only want a toolbar in my activity and a few fragments, so I'm willing to set it manually for those. 我在AndroidManifest.xml NoActionBar应用程序主题设置为NoActionBar ,因为我只想在活动中添加工具栏和一些片段,因此我愿意为这些对象手动设置它。

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>

I made a view for my toolbar tb.xml 我为工具栏tb.xml了一个视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<TextView
    android:id="@+id/tvTitle"
    android:text="Main"
    android:textColor="@color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:layout_gravity="center" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivBell"
    android:background="@drawable/bell"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true" />
</android.support.v7.widget.Toolbar>

and here's how I set it in the activity: 这是我在活动中设置的方法:

 Toolbar toolbar = (Toolbar) getLayoutInflater().inflate(R.layout.main_toolbar, null);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayShowTitleEnabled(false);

But it doesn't show up when I launch my app - any ideas what the problem could be? 但是当我启动我的应用程序时,它没有显示-任何想法可能是什么问题?

Try below code: 试试下面的代码:

In your style.xml 在你的style.xml

    <!-- 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/colorAccent</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:splitMotionEvents">false</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

In your AndroidMenifest.xml 在您的AndroidMenifest.xml

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

In your Activity.java or BaseActivity.java file add following method and call it in onCreate or you can directly put method's code in Activity.java's onCreate : 在您的Activity.javaBaseActivity.java文件中,添加以下方法并在onCreate调用它,或者您可以将方法的代码直接放入Activity.java的onCreate

public void setUpToolbar(String strTitle) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            title = (TextView) toolbar.findViewById(R.id.tvTitle);
            title.setText(strTitle);
        }
    }

Note: Don't forget to include tb.xml file in your activity's .xml file 注意:不要忘记在活动的.xml文件中包含tb.xml文件

In your Fragment.java or BaseFragment.java file add following method and call it in onCreateView or you can directly put method's code in Fragment.java's onCreateView : Fragment.javaBaseFragment.java文件中,添加以下方法并在onCreateView调用它,或者您可以将方法的代码直接放入Fragment.java的onCreateView

public void setupToolBarWithBackArrow(Toolbar toolbar, @Nullable String Title) {
        ActionBar actionBar;
        // If you are using BaseActivity.java then keep below code as it is otherwise replace BaseActivity with Your Activity's name
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        actionBar = ((BaseActivity) getActivity()).getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_back); // Set null if you don't want to show back arrow
        }
        // Remove this click listener if you set null in setHomeAsUpIndicator(...)
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.onBackPressed();
            }
        });
        title = (TextView) toolbar.findViewById(R.id.title);
        title.setText(Title != null ? Title : "");
    }

Note: Don't forget to include tb.xml file in your fragment's .xml file 注意:不要忘记在片段的.xml文件中包含tb.xml文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM