[英]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.