我想在我的活動中實現一個工具欄,但它似乎不工作。我的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); <------------------
它窩如果有人能夠幫助我,我真的很感激它。祝你今天愉快!
維達爾
您是否試過[本教程](https://developers.google.com/android/reference/com/google/android/gms/ads/AdView)?說你應該手動設置大小。 – flakes