简体   繁体   English

(Android / Xamarin)将ImageButtons添加到LinearLayout(动态地)

[英](Android/Xamarin)Add ImageButtons to LinearLayout (Dynamicly)

i am trying to add ImageButtons dynamicly to my LinearLayout, which is created dynamicly as well. 我试图将ImageButtons动态添加到我的LinearLayout中,该动态创建也是动态的。

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: 您可以创建一个ImageButton并将其添加到视图中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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