在 Xamarin.Android 中的布局之间移动

Move between layouts in Xamarin.Android

我正在使用 Xamarin Android 开发应用程序。我想使用一个按钮,然后移动到下一个布局(我有 3 个布局)。我可以从布局 1 移动到布局 2,但不能从布局 2 移动到布局 3。

布局一

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
    android:background="#1f1e1e"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">

    <Button
        android:text="One"
        android:clickable="true"
        android:id="@+id/btnOne"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:backgroundTint="#484848"
        android:layout_marginTop="3dp"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"/>

 </LinearLayout>

////////////////////////////////

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.LayoutOne);

        Button btnOne = FindViewById<Button>(Resource.Id.btnOne);
        btnOne.Click += btnOne_Click;
    }
private void btnOne_Click(object Sender, EventArgs e)
    {
       SetContentView(Resource.Layout.LayoutTwo);
    }

LayoutTwo

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
    android:background="#1f1e1e"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">

    <Button
        android:text="Two"
        android:clickable="true"
        android:id="@+id/btnTwo"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:backgroundTint="#484848"
        android:layout_marginTop="3dp"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"/>

 </LinearLayout>

////////////////////////////////

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.LayoutTwo);

        Button btnTwo = FindViewById<Button>(Resource.Id.btnTwo);
        btnTwo.Click += btnTwo_Click;
    }
private void btnTwo_Click(object Sender, EventArgs e)
    {
       SetContentView(Resource.Layout.LayoutThree);
    }

布局三

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
    android:background="#1f1e1e"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
        android:textSize="16sp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="This is Layout3"
        android:layout_gravity="center_horizontal"
        android:textColor="@android:color/white"/>

 </LinearLayout>

没有错误,应用 运行s。我 运行 通过我的 phone 使用 USB 调试。

如果你想在三种不同的布局之间导航,一种常见的方法是创建三个不同的活动并为它们设置三种布局。 例如,FirstActivity加载LayoutOneSecondActivity加载LayoutTwoThirdActivity加载LayoutThree,之后我们可以使用Intent来在它们之间导航。

请参考以下代码:

FirstActivity.cs

public class FirstActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.LayoutOne);

        Button btnOne = FindViewById<Button>(Resource.Id.btnOne);
        btnOne.Click += BtnOne_Click;
    }

    private void BtnOne_Click(object sender, System.EventArgs e)
    {
        // SetContentView(Resource.Layout.LayoutTwo);

        Intent intent = new Intent(this,typeof(SecondActivity));

        StartActivity(intent);
    }
}

SecondActivity.cs

public class SecondActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.LayoutTwo);

        Button btnTwo = FindViewById<Button>(Resource.Id.btnTwo);
        btnTwo.Click += btnTwo_Click;


    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        //SetContentView(Resource.Layout.LayoutThree);

        Intent intent = new Intent(this,typeof(ThirdActivity));
        StartActivity(intent);
    }
}

ThirdActivity.cs

public class ThirdActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

       SetContentView(Resource.Layout.LayoutThree);
    }
}