如何停止 Xamarin Android StackView 元素中的项目重叠?

Posted

技术标签:

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

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

StackView 元素

        <StackView 
            android:id="@+id/stackViewMaterials"  
            android:layout_
            android:layout_>
        </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;
                

            

【问题讨论】:

【参考方案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_
android:layout_>

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_
android:layout_>

型号:

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

【讨论】:

以上是关于如何停止 Xamarin Android StackView 元素中的项目重叠?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xamarin/Android 上启用多个 BLE 特征通知?

如何在 Xamarin Android 中剪辑滚动视图的边界

如何在 Xamarin 中打开默认浏览器?

Android Xamarin-使用SQLite时打开计划的通知会停止重复并崩溃

如何停止 Xamarin.UITest 删除测试之间的数据?

xamarin.android 接收器出现 BOOT_COMPLETED 错误