我正在尝试从MvxTabActivity迁移到新的ViewPager。问题在于,当您执行此操作时,您不再拥有默认的ActionBar,并且必须在布局中定义一个。这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
local:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
local:tabGravity="center"
local:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
local:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这是我的活动:
[Activity(Label = "PayItemView", Theme = "@style/MPSTheme")]
public class PayItemView : MvxFragmentActivity
{
PayItemViewModel vm;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Android.Support.V7.Widget.Toolbar myToolbar = (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.toolbar);
myToolbar.Title = "Current Pay Summary - " + Common.Settings.DriverName;
}
protected override void OnViewModelSet()
{
base.OnViewModelSet();
SetContentView(Resource.Layout.PayItemView);
vm = (this.DataContext as PayItemViewModel) ?? null;
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>
{
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("OTR", typeof (OTRTabView),
typeof (OTRTabViewModel)),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("LPP", typeof (LPPTabView),
typeof (LPPTabViewModel)),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("Hours", typeof (HourTabView),
typeof (HourTabViewModel)),
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, fragments);
}
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
}
}
我创建了我的风格如下:
<style name="MPSTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#3f51b5</item>
</style>
我在Style的colorPrimary属性中添加了一个随机颜色,它可以正常工作。问题是我的应用程序的其余部分使用ActionBar的设备默认颜色。如果您不使用基于Theme.AppCompat的主题,则不会使您的视图膨胀。有没有办法让我的主题基于Theme.AppCompat并使用设备默认主题中的colorPrimary?
答案 0 :(得分:0)
尝试将其包装在android.support.design.widget.AppBarLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
local:tabIndicatorColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
local:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>