在xamarin.android中管理屏幕旋转时的片段外观

问题描述

在我以为我终于完成了我的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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" 
    android:layout_marginBottom="40dp"
    android:textAlignment="center"
    android:textColor="@android:color/holo_blue_dark"/>
        <EditText
        android:layout_width="match_parent"
        android:hint="Account"
        android:id="@+id/editText1"
    android:background="@drawable/logincustom"
    android:layout_height="200px"
     android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"/>
     <EditText
        android:layout_width="match_parent"
        android:hint="Password"
        android:id="@+id/editText2"
    android:layout_marginTop="20dp"
    android:layout_height="200px"
    android:background="@drawable/logincustom"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"/>
    <Button
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Login"
         android:layout_marginTop="30dp"
        android:background="@drawable/buttoncustom"
    />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        android:id="@+id/checkBox1"/>
        <TextView
            android:text="Keep me logged in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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",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
        }

       
    }
}

下图中的

是旋转手机屏幕时发生的情况:

enter image description here

enter image description here

我该怎么办?

解决方法

您可以创建两种布局,一种用于纵向,另一种用于横向。然后,旋转屏幕时,它将根据您的需要显示。

例如,“布局”文件夹如下:

enter image description here

然后从活动中删除ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize

然后,旋转手机屏幕时,布局将更改。