简体   繁体   中英

Toolbar not showing up?

I am trying to implement a toolbar in my activity but it doesn't seem to work. My MainActivity is a ViewPager, which contains Fragments 1 and 2. I tried to implement my toolbar directly into the Viewpager, this didn't work so instead I tried to wrap my Viewpager in a RelativeLayout and include the toolbar in that RelativeLayout, but this also doesn't work.

The layout for my toolbar (app_bar.xml):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/app_bar"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/CustomToolBarStyle"
android:title="Something"
android:logo="@drawable/ic_launcher">

</android.support.v7.widget.Toolbar>

this is how I've implemented the toolbar in my activity (activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<include android:id="@+id/app_bar" layout="@layout/app_bar"/>   <-----------

    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>

        </android.support.v4.view.ViewPager>

        </RelativeLayout>

and this is what I've done in my activity's Java file to set the toolbar as the actionbar (MainActivity.java):

public class MainActivity extends AppCompatActivity {

ViewPager pager;
android.support.v4.app.FragmentManager fragmentManager;
FloatingActionButton fab;
Toolbar tb;

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

    pager = (ViewPager) findViewById(R.id.pager);
    fab = (FloatingActionButton) findViewById(R.id.fab_main);
    tb = (Toolbar) findViewById(R.id.app_bar);
    fragmentManager = getSupportFragmentManager();
    pager.setAdapter(new myAdapter(fragmentManager));

    AdView mAdView = (AdView) findViewById(R.id.adView);
    //AdRequest adRequest = new AdRequest.Builder()
    //.addTestDevice("YOUR_DEVICE_HASH")
    //.build();

    //do not make visible while testing!!
    //mAdView.loadAd(adRequest);
    setSupportActionBar(tb);             <------------------

it would really appreciate it if someone is able to help me out. Have a nice day!

Vidal

尝试从include标记中删除id并将android:layout_below="@+id/app_bar"到您的ViewPager

Change your toolbar height from wrap_content to you ?attr/actionBarSize as shown below:

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/app_bar"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:theme="@style/CustomToolBarStyle"
    android:title="Something"
    android:logo="@drawable/ic_launcher">
</android.support.v7.widget.Toolbar>

and position your viewpager below Toolbar as shown below

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

     <include android:id="@+id/app_bar" layout="@layout/app_bar"/>

     <android.support.v4.view.ViewPager
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:ads="http://schemas.android.com/apk/res-auto"
         android:id="@+id/pager"
         android:layout_below="@id/app_bar"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="#FFFFFF" />

     <com.google.android.gms.ads.AdView
         android:id="@+id/adView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_alignParentBottom="true"
         ads:adSize="BANNER"
         ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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