工具栏不显示?
Toolbar not showing up?
我正在尝试在我的 activity 中实现一个工具栏,但它似乎不起作用。我的 MainActivity 是一个 ViewPager,它包含 Fragments 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 (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>
这就是我在 activity 的 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); <------------------
如果有人能帮助我,我将不胜感激。祝你有美好的一天!
比达尔
尝试从包含标签中删除 id
,并将 android:layout_below="@+id/app_bar"
添加到您的 ViewPager
。
将工具栏高度从 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>
希望对您有所帮助!
我正在尝试在我的 activity 中实现一个工具栏,但它似乎不起作用。我的 MainActivity 是一个 ViewPager,它包含 Fragments 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 (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>
这就是我在 activity 的 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); <------------------
如果有人能帮助我,我将不胜感激。祝你有美好的一天!
比达尔
尝试从包含标签中删除 id
,并将 android:layout_below="@+id/app_bar"
添加到您的 ViewPager
。
将工具栏高度从 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>
希望对您有所帮助!