在类中处理程序布局的最佳方法是什么?

时间:2017-12-21 11:15:06

标签: android android-layout xamarin.android

自从我在这个应用程序上工作以来已经过了几个月。在发布之前,一切都需要完美。 现在越来越多的functunallty被实现,这就是问题所在:现在活动开始达到1000行!为什么这样,你可能会问? 这是因为需要以编程方式设置布局。

在我的应用程序的几乎所有活动中,我们都有一个滚动视图,可以从服务器下载内容,然后将其放入foreach循环中。这使得无法在xmls中设置大部分布局。只是为了吓唬你一点:

    private void DrawLayoutFriends()
    {


        // Start with the header
        frmHeaderFriends.SetBackgroundColor(Color.ParseColor("#38a0c2"));
        frmHeaderFriends.LayoutParameters = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.MatchParent, intHeightOfDisplay / 7);

        ImageButton btnHeader = new ImageButton(this);      
        btnHeader.SetBackgroundResource(Resource.Drawable.mainmenu_btn_news_friends);
        btnHeader.Background.Mutate().SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
        FrameLayout.LayoutParams paramsHeaderBtn = new FrameLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsHeaderBtn.Gravity = GravityFlags.Center;

        TextView txtHeader = new TextView(this);
        txtHeader.Text = "17 news!";
        txtHeader.SetTypeface(font, TypefaceStyle.Normal);
        txtHeader.SetTextColor(Color.ParseColor("#ffffff"));
        FrameLayout.LayoutParams paramsForTextHeader = new FrameLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsForTextHeader.Gravity = GravityFlags.Bottom  | GravityFlags.Right;
        txtHeader.LayoutParameters = paramsForTextHeader;

        btnHeader.LayoutParameters = paramsHeaderBtn;
        frmHeaderFriends.AddView(btnHeader);
        frmHeaderFriends.AddView(txtHeader);



        // Now the loop
        foreach (var friend in newfriends)
        {
            LinearLayout linlayForLoop = new LinearLayout(this);
            LinearLayout.LayoutParams paramsForLinLayLoop = new LinearLayout.LayoutParams
                 (LinearLayout.LayoutParams.WrapContent, intHeightOfDisplay / 2);
            paramsForLinLayLoop.Gravity = GravityFlags.Center;
            linlayForLoop.Orientation = Orientation.Vertical;

            ImageButton btnAvatarOfFriend = new ImageButton(this);

            btnAvatarOfFriend.SetBackgroundResource(Resource.Drawable.general_dummy_avatar_big);
            LinearLayout.LayoutParams paramsForCenter = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
            paramsForCenter.Gravity = GravityFlags.Center;
            paramsForCenter.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
            btnAvatarOfFriend.LayoutParameters = paramsForCenter;

            TextView txtNameOfFriend = new TextView(this);
            txtNameOfFriend.Text = friend.fkUsername.ToUpper()+ " " + 
                                   Resources.GetString(Resource.String.AddedU);
            txtNameOfFriend.TextSize = 18;
            txtNameOfFriend.Gravity = GravityFlags.Center;
            txtNameOfFriend.SetTypeface(font, TypefaceStyle.Normal);
            txtNameOfFriend.SetTextColor(Color.ParseColor("#007762"));
            LinearLayout.LayoutParams paramsForTextFriend = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
            paramsForTextFriend.Gravity = GravityFlags.Center;
            paramsForTextFriend.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
            txtNameOfFriend.LayoutParameters = paramsForTextFriend;

            TextView txtSubLineFriends = new TextView(this);
            txtSubLineFriends.Text = "traveler | lvl 99 | 999,999ap";
            txtSubLineFriends.TextSize = 10;
            txtSubLineFriends.SetTypeface(font, TypefaceStyle.Normal);
            txtSubLineFriends.SetTextColor(Color.ParseColor("#000000"));
            LinearLayout.LayoutParams paramsForTextSubLineFriend = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
            paramsForTextSubLineFriend.Gravity = GravityFlags.Center;
            txtSubLineFriends.LayoutParameters = paramsForTextSubLineFriend;

            linlayForLoop.AddView(btnAvatarOfFriend);
            linlayForLoop.AddView(txtNameOfFriend);
            linlayForLoop.AddView(txtSubLineFriends);

            btnAvatarOfFriend.Click += delegate
            {
                Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
                alert.SetMessage("Hey there! I added you as my friend to follow your profile! Would you like to add me back? :-)");

                alert.SetPositiveButton("Befriend Me!", (senderAlert, args) =>
                {

                    Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
                });

                alert.SetNegativeButton("Visit My Profile!", (senderAlert, args) =>
                {
                    Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
                });

                alert.SetNeutralButton("Dismiss Me!", (senderAlert, args) =>
                {
                    TranslateAnimation animation = new TranslateAnimation(0, -2000, 0, 0);
                    animation.FillAfter = true;
                    animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
                    animation.Duration = intDurationOfAnimation;
                    btnAvatarOfFriend.StartAnimation(animation);
                    txtNameOfFriend.StartAnimation(animation);
                    txtSubLineFriends.StartAnimation(animation);

                    animation.AnimationEnd += delegate
                    {
                        linlayForLoop.RemoveAllViews();
                    };
                });

                Dialog dialog = alert.Create();
                dialog.Show();
            };

            linlayForAddedFriends.AddView(linlayForLoop);
        }



       btnDismissAllLeft.Click += delegate
       {
          // TranslateAnimation animation = new TranslateAnimation(0, -2000, 0, 0);
          // animation.FillAfter = true;
          // animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
          // animation.Duration = intDurationOfAnimation;
          // btnAvatarOfFriend.StartAnimation(animation);
          // txtNameOfFriend.StartAnimation(animation);
          // txtSubLineFriends.StartAnimation(animation);
          //
          // animation.AnimationEnd += delegate
          // {
          //     linlayForLoop.RemoveAllViews();
          // };
        };

      //Put into side drawer

    }

    private void DrawLayoutBolNews()
    {
        // Start with the header
        frmHeaderNews.SetBackgroundColor(Color.ParseColor("#38a0c2"));
        frmHeaderNews.LayoutParameters = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.MatchParent, intHeightOfDisplay / 7);

        ImageButton btnHeader = new ImageButton(this);
        btnHeader.SetBackgroundResource(Resource.Drawable.mainmenu_btn_news_bol);
        btnHeader.Background.Mutate().SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
        FrameLayout.LayoutParams paramsHeaderBtn = new FrameLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsHeaderBtn.Gravity = GravityFlags.Center;

        TextView txtHeader = new TextView(this);
        txtHeader.Text = "17 news!";
        txtHeader.SetTypeface(font, TypefaceStyle.Normal);
        txtHeader.SetTextColor(Color.ParseColor("#ffffff"));
        FrameLayout.LayoutParams paramsForTextHeader = new FrameLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsForTextHeader.Gravity = GravityFlags.Bottom | GravityFlags.Left;
        txtHeader.LayoutParameters = paramsForTextHeader;

        btnHeader.LayoutParameters = paramsHeaderBtn;
        frmHeaderNews.AddView(btnHeader);
        frmHeaderNews.AddView(txtHeader);


        // Now the loop
        LinearLayout linlayForLoop = new LinearLayout(this);
        LinearLayout.LayoutParams paramsForLinLayLoop = new LinearLayout.LayoutParams
             (LinearLayout.LayoutParams.WrapContent, intHeightOfDisplay / 2);
        paramsForLinLayLoop.Gravity = GravityFlags.Center;
        linlayForLoop.Orientation = Orientation.Vertical;

        ImageButton btnNews = new ImageButton(this);
        btnNews.SetBackgroundResource(Resource.Drawable.general_imgv_news);
        LinearLayout.LayoutParams paramsForCenter = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsForCenter.Gravity = GravityFlags.Center;
        paramsForCenter.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
        btnNews.LayoutParameters = paramsForCenter;

        TextView txtInfo = new TextView(this);
        txtInfo.Text = "123 new challenges unlocked!";
        txtInfo.TextSize = 15;
        txtInfo.SetTypeface(font, TypefaceStyle.Normal);
        txtInfo.SetTextColor(Color.ParseColor("#007762"));
        txtInfo.Gravity = GravityFlags.Center;
        LinearLayout.LayoutParams paramsForTextFriend = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
        paramsForTextFriend.Gravity = GravityFlags.Center;
        paramsForTextFriend.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
        txtInfo.LayoutParameters = paramsForTextFriend;

        linlayForLoop.AddView(btnNews);
        linlayForLoop.AddView(txtInfo);

        linlayForLoop.Click += delegate
        {
            Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);              

            alert.SetPositiveButton("Dismiss!", (senderAlert, args) =>
            {
                TranslateAnimation animation = new TranslateAnimation(0, 2000, 0, 0);
                animation.FillAfter = true;
                animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
                animation.Duration = intDurationOfAnimation;
                txtInfo.StartAnimation(animation);
                btnNews.StartAnimation(animation);
                animation.AnimationEnd += delegate
                {
                    linlayForLoop.RemoveAllViews();
                };
            });

            alert.SetNeutralButton("Show!", (senderAlert, args) =>
            {
                Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
            });

            Dialog dialog = alert.Create();
            dialog.Show();
        };

        btnDismissAllRight.Click += delegate
        {
            TranslateAnimation animation = new TranslateAnimation(0, 2000, 0, 0);
            animation.FillAfter = true;
            animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
            animation.Duration = intDurationOfAnimation;
            txtInfo.StartAnimation(animation);
            btnNews.StartAnimation(animation);
            animation.AnimationEnd += delegate
            {
                linlayForLoop.RemoveAllViews();
            };
        };



        //Put into side drawer
        linlayForBolNews.AddView(linlayForLoop);
    }

这只是一个类中两个方法的布局。甚至都不是。

这非常不方便。这个课程不仅篇幅很长,而且读起来也很混乱,因此可以防止出现问题。

现在我的问题:

在我的许多课程/活动中处理程序化布局的最佳方法是什么?

只有一个类有布局功能,然后在需要时来回发送数据?这将使布局脱离活动,但仍然无法阅读。而且,那一个班级很容易达到10k线,这只是荒谬的......

我希望你们在我开始移动所有布局文件然后意识到这是错误之前,能够以正确的方式暗示我。

0 个答案:

没有答案