我正在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);
}
布局二
<?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>
没有错误,应用程序正在运行。我使用usb调试在手机上运行它。
如果您想在三种不同的布局之间导航,一种常见的方法是创建三个不同的活动并为它们设置三种布局。例如,FirstActivity
加载LayoutOne
,SecondActivity
加载LayoutTwo
,ThirdActivity
加载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);
}
}