在 xamarin.android 中管理屏幕旋转时的片段外观
Posted
技术标签:
【中文标题】在 xamarin.android 中管理屏幕旋转时的片段外观【英文标题】:manage fragment appearance upon screen rotation in xamarin.android 【发布时间】:2020-12-11 05:16:06 【问题描述】:当我以为我终于完成了我的 android 应用程序后,我发现当我的手机屏幕旋转时,我的应用程序崩溃了。所以在做了一些研究之后,我发现我必须将这一行添加到我的活动中:
[Activity(Label = "FreeLineApp", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true, NoHistory = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
这行得通,但并不完全。事实上,我的应用程序有一个登录片段,一旦我启动应用程序就会出现。此片段显示在登录活动中,该活动设置为具有空布局的主启动器。当手机屏幕旋转时,这个登录片段不会完全出现。就像它被裁剪一样。 这是我的片段的登录 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:background="@android:color/white"
android:gravity="center"
android:minHeight="1500px"
android:minWidth="1000px"
>
<TextView
android:text="Welcome to FreeLineApp!"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_
android:layout_
android:id="@+id/textView1"
android:layout_marginBottom="40dp"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"/>
<EditText
android:layout_
android:hint="Account"
android:id="@+id/editText1"
android:background="@drawable/logincustom"
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<EditText
android:layout_
android:hint="Password"
android:id="@+id/editText2"
android:layout_marginTop="20dp"
android:layout_
android:background="@drawable/logincustom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<Button
android:layout_
android:layout_
android:id="@+id/button1"
android:text="Login"
android:layout_marginTop="30dp"
android:background="@drawable/buttoncustom"
/>
<RelativeLayout
android:layout_
android:layout_
android:layout_marginTop="20dp"
android:gravity="center">
<CheckBox
android:layout_
android:layout_
android:id="@+id/checkBox1"/>
<TextView
android:text="Keep me logged in"
android:layout_
android:layout_
android:id="@+id/textView2"
android:layout_toRightOf="@+id/checkBox1"
android:layout_marginRight="20dp"
android:textColor="@android:color/holo_blue_dark"
android:layout_marginTop="6dp"
/>
</RelativeLayout>
</LinearLayout>
这是片段出现的我的登录活动:
[Activity(Label = "FreeLineApp", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true, NoHistory = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class LoginActivity : AppCompatActivity,OnLoginInforCompleted
private AlertDialog _dialog;
ISharedPreferences pref = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);
public void inputLoginInforCompleted(string userName, string passWord)
appuser app_user = new appuser()
UserName = user.Username,
Password = user.Password
;
Intent intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("User", JsonConvert.SerializeObject(app_user));
this.StartActivity(intent);
//throw new NotImplementedException();
StartActivity(new Intent(this, typeof(MainActivity)));
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
// StartActivity(typeof(MainActivity));
var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
string userName = pref.GetString("Username", String.Empty);
string password = pref.GetString("Password", String.Empty);
if (userName == String.Empty || password == String.Empty)
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.setOnLoginInforCompleted(this);
dialogFragment.Cancelable = false;
var SupportFragmentManager = this.FragmentManager;
dialogFragment.Show(SupportFragmentManager, "dialog");
else
Intent intent = new Intent(this, typeof(MainActivity));
appuser app_user = new appuser()
UserName = user.Username,
Password = user.Password
;
intent.PutExtra("User", JsonConvert.SerializeObject(app_user));
this.StartActivity(intent);
else
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Connection failed");
alert.SetMessage("Please, check your internet Connection!");
alert.SetNeutralButton("okay", (senderAlert, args) =>
alert.Dispose();
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.setOnLoginInforCompleted(this);
dialogFragment.Cancelable = false;
var SupportFragmentManager = this.FragmentManager;
dialogFragment.Show(SupportFragmentManager, "dialog");
);
_dialog = alert.Create();
_dialog.Show();
// Create your application here
以下图片是手机屏幕旋转时发生的情况:
我该怎么办?
【问题讨论】:
【参考方案1】:您可以创建两种布局,一种用于Portrait,另一种用于Landscape。然后当屏幕旋转时,它会显示为你想要的。
例如,Layout文件夹如下:
并从Activity中删除ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize
。
那么当手机屏幕旋转时布局会发生变化。
【讨论】:
以上是关于在 xamarin.android 中管理屏幕旋转时的片段外观的主要内容,如果未能解决你的问题,请参考以下文章
MvvmCross Xamarin Android 在初始屏幕上挂起并带有链接
如何在 Xamarin Android 的屏幕上显示全尺寸视图?
使用 LinearLayout 向 ListView 添加标题,Listview 被推离屏幕(Xamarin Android)