[英]FindViewById on Android Subpage (using Xamarin.Forms)
我正在使用Xamarin.Forms开发跨平台应用程序。 因为我想将TabLayouts的背景设置为渐变,XF本身不支持该渐变,所以我为TabbedPages编写了一个自定义渲染器:
using Android.App;
using Android.Graphics;
using Android.Support.Design.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using MyApp.Droid.Renderer;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace MyApp.Droid.Renderer
{
public class CustomTabbedPageRenderer : TabbedPageRenderer
{
private Activity _activity;
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
_activity = this.Context as FormsAppCompatActivity;
}
protected override void DispatchDraw(Canvas canvas)
{
base.DispatchDraw(canvas);
TabLayout tabs = _activity.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
var gradient = new MyGradient();
tabs.SetBackground(gradient);
}
}
}
现在按预期,每次应用程序加载TabbedPage时,都会触发渲染器。 但是, _activity.FindViewById<TabLayout>(Resource.Id.sliding_tabs)
总是只返回我的MainPage的TabLayout(也将MyGradient正确设置为背景),我不知道如何在我的一个子页面上获取TabLayout进行修改它也是。
不确定这是否与Xamarin或Android有关...
希望你们能帮助我。
谢谢!
一月
编辑:这是styles.xml(我希望这是@ Code-Apprentice所指的):
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<!--<item name="colorPrimaryDark">#1976D2</item>-->
<item name="colorPrimaryDark">#FF8630</item>
<!--<item name="android:windowTranslucentStatus">true</item>-->
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
也是MainPage顶部的屏幕截图: http : //i.imgur.com/VyX3YX7.png
它是子页面(MainPage.Navigation.PushAsync(new SubPage());): http ://i.imgur.com/pRmecjE.png
我确实(通过渲染器)在我的应用程序中都获得了两个TabbedPages,但是我需要的是TabLayout(顶部的栏,其中包含Android上的标签)。
如果我这次正确理解了您的问题,则想在TabbedPage
为每个子页面自定义选项卡布局。
然后,您将需要重写TabbedPageRenderer
的SetTabIcon
方法。 例如:
[assembly: ExportRenderer(typeof(MyTabbedPage), typeof(MyTabbedPageRenderer))]
namespace YourNameSpace.Droid
{
public class MyTabbedPageRenderer : TabbedPageRenderer
{
protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
{
base.SetTabIcon(tab, icon);
//set your custom layout for tab. The layout resource "mytablayout" is placed in the layout folder of android project.
tab.SetCustomView(Resource.Layout.mytablayout);
}
}
}
您也可以检查我在此问题中的答案,在此选项卡中放置了一个“ Button
”以删除匹配的子页面。
对于任何其他想知道的人,这就是我现在想要的结果的方式(即使我认为这不是最优雅的方式):
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using MyApp.Droid.Renderer;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace MyApp.Droid.Renderer
{
public class CustomTabbedPageRenderer : TabbedPageRenderer
{
protected override void OnVisibilityChanged(Android.Views.View changedView, [GeneratedEnum] ViewStates visibility)
{
base.OnVisibilityChanged(changedView, visibility);
if (visibility == ViewStates.Visible)
{
var tabs = changedView.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
var gradient = new MyGradient();
tabs.SetBackground(gradient);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.