gpt4 book ai didi

android - 在基于TabLayout的应用程序上实现屏幕旋转

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:47 24 4
gpt4 key购买 nike

我很确定这是一个安卓系统的问题;Xamarin部分只是偶然的。
我有一个简单的基于Xamarin.android的应用程序。它是基于Xamarin的SupportLibFragments sample,但我添加了一个表格。我想知道如何处理屏幕方向的变化。现在,如果你改变屏幕方向,应用程序根本不会做出反应。我甚至不知道从哪里开始。我将在下面发布关键代码,整个项目都可用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>

最佳答案

我尝试了这个应用程序,它是旋转的,我在2个模拟器(Nexus5,通用Android7)和2个物理设备(Samsungs7Edge和OneplusOne)上测试了它。
再次检查系统旋转设置!

关于android - 在基于TabLayout的应用程序上实现屏幕旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com