簡體   English   中英

(Android / Xamarin)將ImageButtons添加到LinearLayout(動態地)

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

我試圖將ImageButtons動態添加到我的LinearLayout中,該動態創建也是動態的。

我的代碼:

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

圖片以便更好地理解:

外觀(繪畫): 外觀

它如何騎行大聲笑: 它如何騎行

您可以創建一個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