工具栏没有显示?

时间:2016-03-01 13:28:02

标签: java android android-layout toolbar

我正在尝试在我的活动中实现一个工具栏,但它似乎不起作用。我的MainActivity是一个ViewPager,它包含片段1和2.我试图将我的工具栏直接实现到Viewpager中,这没有用,所以我尝试将我的Viewpager包装在RelativeLayout中,并在RelativeLayout中包含工具栏,但这也行不通。

我的工具栏的布局(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>

这就是我在我的活动(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>

这就是我在我的活动的Java文件中将工具栏设置为操作栏(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);             <------------------
如果有人能够帮助我,我会非常感激。祝你有愉快的一天!

维达尔

2 个答案:

答案 0 :(得分:0)

尝试从包含标记中删除id,然后将android:layout_below="@+id/app_bar"添加到ViewPager

答案 1 :(得分:0)

将工具栏高度从wrap_content更改为?attr/actionBarSize,如下所示:

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>

并将您的viewpager定位在Toolbar下方,如下所示

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>

希望这有帮助!