简体   繁体   中英

(Android/Xamarin)Add ImageButtons to LinearLayout (Dynamicly)

i am trying to add ImageButtons dynamicly to my LinearLayout, which is created dynamicly as well.

My Code:

foreach (var siteInfo in sites)
        {
            var ll = new LinearLayout(this);
            ll.LayoutParameters = paramProjectElement;
            ll.SetBackgroundColor(Color.ParseColor("#FFFFFF"));
            ll.SetMinimumHeight(UiHelper.GetDpInPixel(40));
            ll.Orientation = Orientation.Horizontal;

            var textElement = new TextView(this);
            textElement.LayoutParameters = paramSiteAndKeywordElement;
            textElement.Text = siteInfo.Uri;
            textElement.SetTextSize(ComplexUnitType.Dip, 22);
            textElement.SetTextColor(Color.ParseColor("#212121"));
            textElement.SetPadding(UiHelper.GetDpInPixel(10), 0, 0, 0);

            ll.AddView(textElement);

            var buttonRefresh = new Button(this);
            buttonRefresh.SetBackgroundResource(Resource.Drawable.Refresh);
            buttonRefresh.LayoutParameters = paramButtons;
            buttonRefresh.SetBackgroundColor(Color.ParseColor("#000000"));

            ll.AddView(buttonRefresh);

            contentLayout.AddView(ll);

            ll = new LinearLayout(this);
            ll.LayoutParameters = paramSiteAndKeywordElement;
            ll.SetBackgroundColor(Color.Black);
            ll.SetMinimumHeight(UiHelper.GetDpInPixel(1));

            contentLayout.AddView(ll);

            foreach (var siteInfoKeyWord in siteInfo.KeyWords)
            {
                ll = new LinearLayout(this);
                ll.LayoutParameters = paramSiteAndKeywordElement;
                ll.SetBackgroundColor(Color.ParseColor("#FFFFFF"));
                ll.SetMinimumHeight(UiHelper.GetDpInPixel(50));
                ll.Orientation = Orientation.Horizontal;

                textElement = new TextView(this);
                textElement.LayoutParameters = paramSiteAndKeywordElement;
                textElement.Text = siteInfoKeyWord;
                textElement.SetTextSize(ComplexUnitType.Dip, 18);
                textElement.SetTextColor(Color.ParseColor("#757575"));
                textElement.SetPadding(UiHelper.GetDpInPixel(10), 0, 0, 0);

                ll.AddView(textElement);
                contentLayout.AddView(ll);

                ll = new LinearLayout(this);
                ll.LayoutParameters = paramSiteAndKeywordElement;
                ll.SetBackgroundColor(Color.ParseColor("#BDBDBD"));
                ll.SetMinimumHeight(UiHelper.GetDpInPixel(1));

                contentLayout.AddView(ll);
            }

Pictures for better understanding:

How it should look (Paint): how it should look

How it rly lool: How it rly is

You can create a ImageButton and add it to your view like this:

        var btn = new ImageButton(this);
        btn.SetImageResource(...);
        btn.SetOnClickListener(...);
        btn.SetBackgroundColor(Color.AliceBlue);
        // add more propterties
        contentLayout.AddView(btn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM