A
A
Alexander2021-09-07 16:06:22
Android
Alexander, 2021-09-07 16:06:22

How to set padding and position for elements created dynamically from code?

The problem with the position of the elements that are generated from the code. I'm trying to give them a center position and a margin (left to right) from the previous element. I tried using LinearLayout and RowLayout, but the results are the same (with linearlayer, the elements are on the left and do not even react to the center, but do not crawl over each other). YhKhI.pnghow I'm trying to do it.
I tried to play through 2 parameters Layout, gravity and gravity for elements.

RelativeLayout relativeLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
            RelativeLayout.LayoutParams relativeLayoutParams2 = new RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
            relativeLayoutParams.SetMargins(30,20,30,0);
            relativeLayoutParams.AddRule(LayoutRules.CenterHorizontal);




            #region team1 Elements
            ImageView firstTeamLogo = new ImageView(this);
            firstTeamLogo.LayoutParameters = relativeLayoutParams;

            TextView firstTeamText = new TextView(this);
            firstTeamText.Text = "testTeam1";
            firstTeamText.LayoutParameters = relativeLayoutParams;

            var firstTeamLogoBitmap = ImageDownloader.GetImageBitmapFromUrl("");
            firstTeamLogo.SetImageBitmap(firstTeamLogoBitmap);
            #endregion

            #region team2 Elements
            ImageView secondTeamLogo = new ImageView(this);
            secondTeamLogo.LayoutParameters = relativeLayoutParams;

            TextView secondTeamText = new TextView(this);
            secondTeamText.Text = "testTeam2";
            secondTeamText.LayoutParameters = relativeLayoutParams;

            var secondTeamLogoTeamLogoBitmap = ImageDownloader.GetImageBitmapFromUrl("");
            secondTeamLogo.SetImageBitmap(secondTeamLogoTeamLogoBitmap);
            #endregion

            TextView versusText = new TextView(this);
            versusText.Text = "vs";
            versusText.LayoutParameters = relativeLayoutParams;




            relativeLayout.AddView(firstTeamLogo);
            relativeLayout.AddView(firstTeamText);
            relativeLayout.AddView(versusText);
            relativeLayout.AddView(secondTeamText);
            relativeLayout.AddView(secondTeamLogo);
            SetContentView(relativeLayout, relativeLayoutParams2);

What did I actually do wrong? And the call to draw elements is confusing (it seems like a bit too much duplication), maybe there is a shorter option to do it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question