【问题标题】:How to stop items in Xamarin Android StackView element overlapping?如何停止 Xamarin Android StackView 元素中的项目重叠?
【发布时间】:2021-12-20 06:53:31
【问题描述】:

该图像显示了已添加到 Xamarin Android 应用中的 StackView 的重叠项。如何让它们在不重叠的情况下显示?

StackView 元素

        <StackView 
            android:id="@+id/stackViewMaterials"  
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </StackView>

加载 StackView 的代码。

            if (Arguments != null)
            {
                quote = JsonConvert.DeserializeObject<Quote>(Arguments.GetString("quote"));

                if (Arguments.GetString("work") != null)
                {
                    work = JsonConvert.DeserializeObject<Work>(Arguments.GetString("work"));

                    v.FindViewById<EditText>(Resource.Id.editDescription).Text = work.strDescription;

                    if (work.Materials.Count > 0)
                    {
                        StackView stackMaterials = v.FindViewById<StackView>(Resource.Id.stackViewMaterials);
                        stackMaterials.Adapter = new MaterialAdapter(v.Context, work.Materials);
                        stackMaterials.ItemClick += OnListClick;
                    }

                    v.FindViewById<EditText>(Resource.Id.editWaste).Text = work.dblWaste.ToString();
                    v.FindViewById<EditText>(Resource.Id.editDelivery).Text = work.dblDelivery.ToString();
                    v.FindViewById<EditText>(Resource.Id.editLabour).Text = work.dblLabour.ToString();

                    v.FindViewById<EditText>(Resource.Id.editComment).Text = work.strComment;
                }

            }

【问题讨论】:

    标签: c# android xamarin overlapping stackview


    【解决方案1】:

    您可以设置文本的布局以防止 StackView 项重叠。

    主布局:layout9.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    

    ItemLayout:layout10.xml

      <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    

    型号:

    public class StackItem
    {
        public string itemText1 { get; set; }
        public string itemText2 { get; set; }
        public StackItem(string text1, string text2)
        {
            this.itemText1 = text1;
            this.itemText2 = text2;
        }
    }
    

    适配器:

    public class StackItemAdapter : BaseAdapter<StackItem>
    {
        private List<StackItem> sList;
        private Context sContext;
        public StackItemAdapter(Context context, List<StackItem> list)
        {
    
            this.sList = list;
            this.sContext = context;
        }
    
        public override StackItem this[int position]
        {
            get
            {
                return sList[position];
            }
        }
        public override int Count
        {
            get
            {
                return sList.Count;
            }
    
        }
        public override long GetItemId(int position)
        {
            return position;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;
            if (row == null)
            {
                row = LayoutInflater.From(sContext).Inflate(Resource.Layout.layout10, null, false);
            }
            var m = sList[position];
            if (m != null)
            {
                TextView text1 = row.FindViewById<TextView>(Resource.Id.textView1);
                TextView text2 = row.FindViewById<TextView>(Resource.Id.textView2);
                text1.Text = sList[position].itemText1;
                text2.Text = sList[position].itemText2;
            }
            return row;
        }
    
    }
    

    MainActivity:Activity9

    public class Activity9 : Activity
    {
        private StackView exchangesStack;
        private List<StackItem> stackItems;
        StackItemAdapter adapter;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your application here
            SetContentView(Resource.Layout.layout9);
            stackItems = new List<StackItem>();
            stackItems.Add(new StackItem("Waney Fence Panel","0*1830*1500*50mm 0kg"));
            stackItems.Add(new StackItem("Postmix", "0*0*0*0mm 25kg"));
            stackItems.Add(new StackItem("Recessed Concrete Gravel Board", "0*1830*300*50mm 0kg"));
            stackItems.Add(new StackItem("Comcrete Intermediate Post", "0*100*2400*100mm 0kg"));
            
    
    
            exchangesStack = FindViewById<StackView>(Resource.Id.exchangesStack);
            adapter = new StackItemAdapter(this, stackItems);
            exchangesStack.Adapter = adapter;
            exchangesStack.ItemClick += ExchangesStack_ItemClick;
        }
    
        private void ExchangesStack_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            var select = stackItems[e.Position].itemText1;
            Android.Widget.Toast.MakeText(this, select, Android.Widget.ToastLength.Long).Show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 2017-02-28
      • 2017-05-09
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多