如何在xamarin / android上点击按钮点击2个布局?

时间:2017-12-04 12:25:24

标签: xamarin.android

我开始使用xamarin和android,我不确定理解这个好方法。 然后我创建了一个主要活动和第二个活动及其各自的布局。 在第一个中我放了一个带有该代码的按钮:

button.Click += (sender, e) =>
{
var intent = new Android.Content.Intent(this, typeof(Activity1));
StartActivity(intent);
OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft);
};

在第二个:

button.Click += (sender, e) =>
{
Finish();
};

但是这不能做我想要的,我希望两个布局两个滑动就好像并排,但这里第二个布局只是在第一个上滑动。它也来自左边我希望它来自右边而且没有SlideInright。 也是使用两个活动的正确方法,我不能只有一个活动和两个布局(视图?)。

1 个答案:

答案 0 :(得分:0)

首先,您可以使用一个包含两个ViewGroup的活动。至于您的问题,您可以使用包含两个RelativeLayout的{​​{3}}和Animation来完成您想要的内容,例如:

MainActivity.axml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:a="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/rl2"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@android:color/holo_green_dark">
        <Button
            android:id="@+id/bt2"
            android:text="page2"
            android:layout_height="100dp"
            android:layout_width="100dp" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark">
        <Button
            android:id="@+id/bt1"
            android:text="page1"
            android:layout_height="100dp"
            android:layout_width="100dp" />
    </RelativeLayout>
</FrameLayout>

MainActivity:

        [Activity(Label = "Aniamtion", MainLauncher = true)]
        public class MainActivity : Activity
        {
            Button bt1,bt2;
            RelativeLayout rl1, rl2;
            int width;
            protected override void OnCreate(Bundle savedInstanceState)
        {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        getScreenWidth();//get the screen's width, so we can define how much the animation can translate
        initView();
        initListener();
        }

        private void getScreenWidth()
        {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager.DefaultDisplay.GetMetrics(dm);
            width = dm.WidthPixels;
        }


        private void initListener()
        {
            bt1.Click += toPage1;
            bt2.Click += toPage2;
        }

        private void toPage1(object sender, EventArgs e)
        {
            //use ObjectAnimator to complete it
            ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", 0, -width);
            objectAnimator.SetDuration(500);
            objectAnimator.Start();
            ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", width, 0);
            objectAnimator1.SetDuration(500);
            objectAnimator1.Start();
        }

        private void toPage2(object sender, EventArgs e)
        {
            ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", -width, 0);
            objectAnimator.SetDuration(500);
            objectAnimator.Start();
            ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", 0, width);
            objectAnimator1.SetDuration(500);
            objectAnimator1.Start();
        }

        private void initView()
        {
            bt1 = FindViewById<Button>(Resource.Id.bt1);
            bt2 = FindViewById<Button>(Resource.Id.bt2);
            rl1 = FindViewById<RelativeLayout>(Resource.Id.rl1);
            rl2 = FindViewById<RelativeLayout>(Resource.Id.rl2);
        }
        }

其次,关于此FrameLayout方法,您可以使用OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft)的参数,或者System resource

更新

将此信息放入您的资源 - >动画 - &gt; SlideInRight,custom itthis is about customing animation in Xamarin.Android, it is same as Android

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
  <translate android:fromXDelta="50%p" 
             android:toXDelta="0"
            android:duration="300"/>
</set>