问题
I'm adding a floating action button to my app, I have a material design theme and set it into the Android Manifest, I've already set the AppCompatActivity inheritance to the MainActivity but if I run my app I have an exception says " You need to use a Theme.AppCompat theme (or descendant) with this activity."
This is my main activity:
using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Views;
namespace M_v1
{
[Activity(Label = "Muzzillo_v1", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
Android.Support.V7.Widget.Toolbar toolbar;
private FloatingActionButton fabMain;
private View bgFabMenu;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
TabLayout tabLayout = (TabLayout)FindViewById(Resource.Id.tablayout_navigation);
ViewPager viewPager = (ViewPager)FindViewById(Resource.Id.pager);
SetupviewPager(viewPager);
fabMain = FindViewById<FloatingActionButton>(Resource.Id.fab);
bgFabMenu = FindViewById<View>(Resource.Id.bg_fab_menu);
fabMain.Click += FabMain_Click;
tabLayout.SetupWithViewPager(viewPager);
toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "App_v1";
}
private void FabMain_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void SetupviewPager(ViewPager viewPager)
{
viewPager.OffscreenPageLimit = 2;
PageAdapter adapter = new PageAdapter(SupportFragmentManager);
adapter.AddFragment(new Fragment1(), "One");
adapter.AddFragment(new Fragment2(), "Two");
viewPager.Adapter = adapter;
}
}
}
And this is my Theme:
<resources>
<style name = "AppTheme.Base" parent = "Theme.AppCompat.Light.NoActionBar">
<item name ="colorPrimary">@color/primary</item>
<item name ="colorPrimaryDark">@color/primaryDark</item>
<item name="android:windowBackground">@color/window_background</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
</style>
</resources>
Android Manifest:
android:theme="@style/AppTheme"
回答1:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
There are two solutions to solve your problem :
- Set
Activity
theme in Application scope inManifest.xml
(all activities) - Or add a
Theme
attribute inActivity
.
1. Theming an Application
Set Activity
theme in Application scope in Manifest.xml
(all activities) :
<application
android:label="@string/app_name"
android:icon="@drawable/Icon"
android:theme="@style/AppTheme">
</application>
2. Theming an Activity
In Xamarin.Android, it is recommended to add the Activity theme as an attribute. You could read the document Material Theme :
To theme an activity, you add a Theme setting to the [Activity] attribute above your activity declaration and assign Theme to the Material Theme flavor that you want to use. The following example themes an activity with Theme.Material.Light:
For example :
[Activity(Label = "Muzzillo_v1", MainLauncher = true, Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity
来源:https://stackoverflow.com/questions/48174193/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity-xa