2016-03-01 242 views
0

我想在我的活動中實現一個工具欄,但它似乎不工作。我的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);    <------------------ 

它窩如果有人能夠幫助我,我真的很感激它。祝你今天愉快!

維達爾

+0

您是否試過[本教程](https://developers.google.com/android/reference/com/google/android/gms/ads/AdView)?說你應該手動設置大小。 – flakes

回答

1

嘗試刪除從id包括標籤,並添加android:layout_below="@+id/app_bar"ViewPager

+0

謝謝!不知何故,它的工作。狀態欄(包含電池電量百分比,wifi狀態等)與工具欄重疊,但我想我會找出如何解決這個問題 –

1

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> 

,並如下圖所示

activity_main.xml中

下面 Toolbar您viewpager定位
<?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> 

希望這可以幫助!

+0

感謝您的解決方案!得到它的工作,但有一個奇怪的錯誤,導致某種形式的工具欄只出現在第一個片段和整個行動條太小 –

+0

如果操作欄的大小太小,那麼你可以隨時設置的寬度和高度你喜歡。只需在dp中設置所需的高度和寬度即可。 – Harry

+0

謝謝!感謝您花時間回答我的問題:) –