【问题标题】:How to make 2 layouts sliding on button click in xamarin/android?如何在 xamarin/android 中单击按钮时使 2 个布局滑动?
【发布时间】:2017-12-11 10:06:49
【问题描述】:

我是从 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。 使用两个活动也是正确的方法,我不是只有一个活动和两个布局(视图)吗?

【问题讨论】:

  • 嗨,我的解决方案对你有用吗?如果对你有用,请标记或回复我,我在等你。
  • 对不起,我没有时间测试,但我会做的,当我有一些......

标签: xamarin.android


【解决方案1】:

首先,您可以使用一个包含两个ViewGroup 的活动。至于你的问题,你可以使用 AnimationFrameLayout 其中包含两个 RelativeLayout 来完成你想要的,像这样:

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>

主活动:

        [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);
        }
        }

第二,关于这个OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft)方法,参数可以用System resource,也可以custom it

更新:

把它放到你的 Resource->anim->SlideInRight、this is about customing animation in Xamarin.Android, it is same as Androidthis is document which you can refer to

<?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>

【讨论】:

  • 关于 OverridePendingTransition() 当你说我可以使用系统资源时是我做的吗?某处没有简单的 SlideInRight 吗?我按照您的链接进行自定义,但它不适用于 xamarin,或者我找不到如何...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多