Xamarin 选项卡不更改数据 Android
Posted
技术标签:
【中文标题】Xamarin 选项卡不更改数据 Android【英文标题】:Xamarin Tabs not changing data Android 【发布时间】:2017-01-25 23:01:47 【问题描述】:请有人帮忙,这正在杀死我!!!我有一个文本视图,由于某种原因它没有更新,我试图让它工作?文本不会改变标签只是空白。
注意:没有错误或任何选项卡都可以正常工作,但文本永远不会更新我似乎不知道为什么请帮忙!
提前致谢
Main.cs:
using System;
using android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace App1
[Activity(Label = "ActionBar Tabs Example")]
public class Main : Activity
protected override void OnCreate(Bundle bundle)
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.TabbedMenuPage);
//enable navigation mode to support tab layout
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
//adding audio tab
AddTab("Audio", Resource.Drawable.Icon, new AudioFragment());
//adding video tab
AddTab("Video", Resource.Drawable.Icon, new VideoFragment());
/*
* This method is used to create and add dynamic tab view
* @Param,
* tabText: title to be displayed in tab
* iconResourceId: image/resource id
* fragment: fragment reference
*
*/
void AddTab(string tabText, int iconResourceId, Fragment fragment)
var tab = this.ActionBar.NewTab();
tab.SetText(tabText);
tab.SetIcon(iconResourceId);
// must set event handler for replacing tabs tab
tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment);
;
this.ActionBar.AddTab(tab);
class AudioFragment : Fragment
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Tab, container, false);
var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
sampleTextView.Text = "This is Audio Fragment Sample";
return view;
class VideoFragment : Fragment
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Tab, container, false);
var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
sampleTextView.Text = "This is Video Fragment Sample";
return view;
Tabaxml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_
android:layout_
android:gravity="center"
android:textStyle="bold"
android:textSize="16dp"
android:background="#ffe3e1e1"
android:textColor="#ff393939" />
TabbedMenu.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_
android:layout_
/>
</LinearLayout>
【问题讨论】:
【参考方案1】:你的问题在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_
android:layout_
/>
</LinearLayout>
您的FrameLayout
的高度为0!这意味着其中的所有内容都被隐藏了。将layout_height
设置为其他值,例如match_parent
。
另外,请注意,fill_parent
现在已弃用。你应该使用match_parent
。
【讨论】:
这解决了它!非常感谢!以上是关于Xamarin 选项卡不更改数据 Android的主要内容,如果未能解决你的问题,请参考以下文章
xml 使用Xamarin.Forms时在Android列表视图中更改所选高亮颜色的示例
如何更改Android中选项卡页面标题的选定和未选定颜色:Xamarin Forms