Xamarin Tabs 不更改数据 Android
Xamarin Tabs not changing data Android
谁能帮帮我,我快死了!!!我有一个文本视图,由于某种原因我正在尝试使用它,但它没有更新?文本没有改变,标签只是空白。
注意:没有错误或任何选项卡都工作正常,但文本从未更新我似乎无法找出原因请帮忙!!
提前致谢
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_width="match_parent"
android:layout_height="match_parent"
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_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
/>
</LinearLayout>
您的问题在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
/>
</LinearLayout>
你的FrameLayout
高度为0!这意味着它里面的所有内容都被隐藏了。将 layout_height
设置为其他内容,例如 match_parent
.
另外,旁注,fill_parent
现在已弃用。你应该使用 match_parent
.
谁能帮帮我,我快死了!!!我有一个文本视图,由于某种原因我正在尝试使用它,但它没有更新?文本没有改变,标签只是空白。
注意:没有错误或任何选项卡都工作正常,但文本从未更新我似乎无法找出原因请帮忙!!
提前致谢
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_width="match_parent"
android:layout_height="match_parent"
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_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
/>
</LinearLayout>
您的问题在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
/>
</LinearLayout>
你的FrameLayout
高度为0!这意味着它里面的所有内容都被隐藏了。将 layout_height
设置为其他内容,例如 match_parent
.
另外,旁注,fill_parent
现在已弃用。你应该使用 match_parent
.