我很确定这是一个Android问题; Xamarin部分只是偶然的。
我有一个简单的基于TabLayout的Xamarin.Android应用程序。它基于Xamarin的SupportLibFragments sample,但我添加了一个TabLayout。我想知道如何处理屏幕方向的变化。现在,如果您更改屏幕方向,应用程序根本不会做出反应。我甚至不知道从哪里开始。我将在下面发布关键代码,整个项目可用on Github。
以下是一些关键文件。首先,activity_main.axml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:theme="@style/Theme.TabBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</LinearLayout>
接下来,MainTabActivity.cs:
using Android.App;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
namespace com.xamarin.sample.fragments.supportlib
{
[Activity(Label = "Fragments Walkthrough", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : FragmentActivity
{
private TabLayout TabLayout => FindViewById<TabLayout>(Resource.Id.tab_layout);
private ViewPager ViewPager => FindViewById<ViewPager>(Resource.Id.viewpager);
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.activity_main);
var manager = SupportFragmentManager;
var adapter = new DetailsAdapter(manager);
ViewPager.Adapter = adapter;
TabLayout.SetupWithViewPager(ViewPager);
adapter.RefreshPageTitles(TabLayout);
}
}
}
接下来,DetailsAdapter.cs:
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Java.Lang;
using System.Linq;
namespace com.xamarin.sample.fragments.supportlib
{
public class DetailsAdapter: FragmentPagerAdapter {
public DetailsAdapter(FragmentManager manager): base(manager) {
}
public override int Count => Shakespeare.Titles.Count(); // Shakespeare is effectively the model class for the app. It contains a bunch of titles and associated text.
public override Fragment GetItem(int position) {
return DetailsFragment.NewInstance(position);
}
public override ICharSequence GetPageTitleFormatted(int position) {
var str = position.ToString();
return new Java.Lang.String(str);
}
internal void RefreshPageTitles(TabLayout tabLayout) {
for (int i = 0; i < Count; i++) {
var tab = tabLayout.GetTabAt(i);
if (tab!=null) {
tab.SetText( GetPageTitleFormatted(i));
}
}
}
}
}
最后,DetailsFragment.cs:
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
namespace com.xamarin.sample.fragments.supportlib
{
internal class DetailsFragment : Fragment
{
public static DetailsFragment NewInstance(int playId)
{
var detailsFrag = new DetailsFragment {Arguments = new Bundle()};
detailsFrag.Arguments.PutInt("current_play_id", playId);
return detailsFrag;
}
public int ShownPlayId
{
get { return Arguments.GetInt("current_play_id", 0); }
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (container == null)
{
return null;
}
var inflatedView = new FrameLayout(this.Activity);
inflatedView.SetBackgroundColor(Android.Graphics.Color.Orange);
var text = new TextView(this.Context);
inflatedView.AddView(text);
var padding = 10;
text.SetPadding(padding, padding, padding, padding);
text.TextSize = 24;
text.Text = Shakespeare.Dialogue[ShownPlayId]; // just returns a bunch of text
return inflatedView;
}
}
}
编辑:我尝试在清单中启用旋转,如下所示,并删除MainActivity类上的属性。它没有帮助;当我旋转手机时,我的活动中没有调用OnDestroy。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<application android:icon="@drawable/icon">
<activity
android:name="com.xamarin.sample.fragments.supportlib.MainActivity"
android:label="Fragments Walkthrough"
android:icon="@drawable/icon"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
</manifest>
答案 0 :(得分:3)
我尝试了该应用程序,然后旋转了该程序,并在2个模拟器(Nexus 5,通用Android 7)和2个物理设备(Samsung S7 Edge和Oneplus One)上进行了测试
仔细检查系统旋转设置!