国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Xamarin.Android實現(xiàn)界面自動添加控件

這篇具有很好參考價值的文章主要介紹了Xamarin.Android實現(xiàn)界面自動添加控件。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、背景說明

有時需要在APP中動態(tài)的添加控件,因此記錄下在Xamarin中的實現(xiàn)步驟。
VS2022社區(qū)版

2、效果

Xamarin.Android實現(xiàn)界面自動添加控件,Xamarin,xamarin,android

3、代碼

3.1、UI代碼

UI的代碼如何

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">  <!--注意,這兒的權(quán)重是1-->

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/linearlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:isScrollContainer="false"
                android:padding="10dp">

                <!--下面這個布局主要是方便計算的-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:baselineAligned="true"
                    android:orientation="horizontal">

                </LinearLayout>

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#34ab8b"
            android:layout_margin="10dip"
            android:text="添加"
            android:textSize="18dp"
            android:textColor="#fff"/>
        <Button
            android:id="@+id/btn_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#34ab8b"
            android:layout_margin="10dip"
            android:text="編輯"
            android:textSize="18dp"
            android:textColor="#fff"/>
    </LinearLayout>

</LinearLayout>

3.2、實現(xiàn)代碼

活動中的代碼如下:

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.AppCompat.App;
using Google.Android.Material.FloatingActionButton;
using Google.Android.Material.Snackbar;
using Android.Widget;
using Java.Lang;
using System.Collections.Generic;
using System.Drawing;

namespace DynamicAddControls
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {

        private LinearLayout linearLayout;
        //Button索引
        private List<Button> ListBtn_Show;
        //TextView索引
        private List<TextView> ListText_Def;
        private Button btn_add, btn_edit;
        //判斷btn_edit的狀態(tài)
        private int EDITSTATE = 0;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.Click += FabOnClick;

            inited();
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu_main, menu);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            int id = item.ItemId;
            if (id == Resource.Id.action_settings)
            {
                return true;
            }

            return base.OnOptionsItemSelected(item);
        }

        private void FabOnClick(object sender, EventArgs eventArgs)
        {
            View view = (View) sender;
            Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
                .SetAction("Action", (View.IOnClickListener)null).Show();
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }



        private void inited()
        {
            linearLayout = (LinearLayout)FindViewById(Resource.Id.linearlayout);
            ListBtn_Show = new List<Button>();
            ListText_Def = new List<TextView>();
            btn_edit = (Button)FindViewById(Resource.Id.btn_edit);
            btn_add = (Button)FindViewById(Resource.Id.btn_add);
            btn_add.Click += Btn_add_Click;
            btn_edit.Click += Btn_edit_Click;


        }

        private void Btn_edit_Click(object sender, EventArgs e)
        {
            if (EDITSTATE == 0)
            {
                btn_edit.Text = "確定";
                EDITSTATE = 1;
            }
            else if (EDITSTATE == 1)
            {
                btn_edit.Text = "編輯";
                EDITSTATE = 0;
            }
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            addBtn();//動態(tài)添加按鈕
        }

        private void addBtn()
        {//動態(tài)添加按鈕
         //添加承載兩個按鈕的LinearLayout
            LinearLayout linear_layout = new LinearLayout(this);
            linear_layout.Orientation= Orientation.Horizontal;//在同一個linearLayout中水平放置兩個控件
            LinearLayout.LayoutParams liParam = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MatchParent,
                    ViewGroup.LayoutParams.WrapContent);
            linear_layout.LayoutParameters = liParam;

            //添加左側(cè)的Button
            Button btnShow = new Button(this);
            LinearLayout.LayoutParams btnAddPar = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WrapContent, 
                108, 3);//設(shè)置寬高及占比
            btnAddPar.SetMargins(0, 10, 0, 10);
            btnShow.LayoutParameters=btnAddPar;
            //設(shè)置文本及顏色
            btnShow.Text=ListBtn_Show.Count + ""+ linear_layout.Id;
            btnShow.SetTextColor(Android.Graphics.Color.Argb(255, 255, 255, 255));
            btnShow.SetBackgroundColor(Android.Graphics.Color.Argb(255, 52, 171, 139));
            btnShow.Click += BtnShow_Click;//注冊事件
            linear_layout.AddView(btnShow);//把btnShow添加到linear_btn中
            ListBtn_Show.Add(btnShow);//把btnShow添加到索引中
            

            //添加右側(cè)的TextView
            TextView textDef = new TextView(this);
            //設(shè)置文本的格式
            LinearLayout.LayoutParams btnDefPar = new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.WrapContent, 108, 1);//設(shè)置寬高及占比
            btnDefPar.SetMargins(0, 10, 0, 10);
            textDef.LayoutParameters=btnDefPar;

            textDef.Text="設(shè)為默認";
            textDef.Gravity=GravityFlags.Center;
            textDef.SetTextColor(Android.Graphics.Color.Argb(255, 255, 255, 255));
            textDef.SetBackgroundColor(Android.Graphics.Color.Argb(255, 52, 171, 139));
            textDef.Click += TextDef_Click;

  

            linear_layout.AddView(textDef);//把textDef添加到linear_btn中
            ListText_Def.Add(textDef);//把textDef添加到索引中
        
            linearLayout.AddView(linear_layout);//把linear_btn添加到外層linearLayout中
        }

        //設(shè)為默認的功能
        private void TextDef_Click(object sender, EventArgs e)
        {
            View view = sender as View;
            setDef(view);//設(shè)置默認
        }

        private void BtnShow_Click(object sender, EventArgs e)
        {
            View view = sender as View;

            if (EDITSTATE == 1) { }
                delBtn(view);//動態(tài)刪除按鈕
        }

        private void setDef(View view)
        {//設(shè)置默認
         //遍歷索引里的所有TextView
            for (int i = 0; i < ListText_Def.Count; i++)
            {
                ListText_Def[i].SetBackgroundColor(Android.Graphics.Color.Argb(255, 52, 171, 139));
                ListText_Def[i].Text="設(shè)為默認";
                if (ListText_Def[i] == view)
                {
                    view.SetBackgroundColor(Android.Graphics.Color.Argb(255, 171, 52, 56));
                    ListText_Def[i].Text="默認";
                }
            }
        }
        private void delBtn(View view)
        {//動態(tài)刪除按鈕
            if (view == null)
            {
                return;
            }
            int position = -1;
            for (int i = 0; i < ListBtn_Show.Count; i++)
            {
                if (ListBtn_Show[i] == view)
                {
                    position = i;
                    break;
                }
            }
            if (position >= 0)
            {
                ListBtn_Show.RemoveAt(position);//從索引中移除被刪除的Button
                ListText_Def.RemoveAt(position);//從索引中移除被刪除的TextView
                linearLayout.RemoveViewAt(position + 1);//在外出linearLayout刪除內(nèi)部指定位置所有控件
            }
        }

    }
}

4、代碼下載

工程代碼

5、相關(guān)知識點

5.1、原理說明

界面中添加控件,就是通過AddView方法實現(xiàn)的,方法有如下的重載版本:

public unsafe virtual void AddView(View? child)
public unsafe virtual void AddView(View? child, LayoutParams? @params)
public unsafe virtual void AddView(View? child, int index)
public unsafe virtual void AddView(View? child, int index, LayoutParams? @params)
public unsafe virtual void AddView(View? child, int width, int height)

參數(shù)child就是要添加的控件元素;LayoutParams代表待添加元素的樣式。但也不一定非得添加這個參數(shù),因為可以在控件的LayoutParameters屬性中設(shè)置樣式。index是索引,從0開始;widthheight設(shè)置控件的大小。具體可以參考上面的代碼。

5.2、其他說明

還有一個方法是直接將樣式文件加載成View,然后再通過addView直接實現(xiàn)

//獲取視圖,其中R.layout.CodeView是xml布局
View v1 =LayoutInflater.from(context).inflate(R.layout.CodeView,null,false);

//添加視圖v1到容器v		
v.addView(v1);

6、參考資料

1、Android筆記——動態(tài)添加刪除控件,及添加點擊事件,本文主要參考這一篇,其介紹通過Android實現(xiàn)動態(tài)加載,本人主要是將其轉(zhuǎn)為了C#版本
2、簡析Android 動態(tài)添加控件的幾種方式
3、關(guān)于 layout_weight,你到底知多少,這一篇主要講述UI中布局的,講得淺顯易懂,推薦文章來源地址http://www.zghlxwxcb.cn/news/detail-617810.html

到了這里,關(guān)于Xamarin.Android實現(xiàn)界面自動添加控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C#使用xamarin進行跨平臺開發(fā)

    使用 Xamarin 進行跨平臺開發(fā)可以使用 C# 和 .NET 平臺來開發(fā)移動應(yīng)用程序,同時將代碼在多個主要移動操作系統(tǒng)上運行,包括 Android 和 iOS。以下是在 C# 中使用 Xamarin 進行跨平臺開發(fā)的一般步驟: 安裝 Xamarin : 在開始之前,你需要安裝 Xamarin 開發(fā)環(huán)境。你可以選擇安裝 Visual

    2024年02月11日
    瀏覽(96)
  • PLC-IoT 網(wǎng)關(guān)開發(fā)札記(6): Xamarin.Forms 的 CollectionView 綁定了什么?

    PLC-IoT 網(wǎng)關(guān)開發(fā)札記(6): Xamarin.Forms 的 CollectionView 綁定了什么?

    項目開發(fā)中不可避免地會遇到在一個頁面中呈現(xiàn)列表的情況,使用 CollectionView 作為容器是很方便的。CollectionView 中顯示的數(shù)據(jù)對應(yīng)于后臺的一個 IEnumerable 派生的列表,常用的是 ListT 和 VectorT,我習(xí)慣于使用 ListT 作為后臺的數(shù)據(jù)表。 CollectionView 的每一項對應(yīng)后臺的 ListT 的一

    2024年01月23日
    瀏覽(29)
  • 第二章 Android控件與界面交互

    第二章 Android控件與界面交互

    Android界面布局就是定義應(yīng)用中的界面結(jié)構(gòu)(例如Activity的界面結(jié)構(gòu))。界面布局中的所有元素均使用View和ViewGroup對象的層次結(jié)構(gòu)進行構(gòu)建。View通常用于繪制用戶可查看并進行交互的內(nèi)容。然而,ViewGroup是不可見容器,用于定義View和其他ViewGroup對象的布局結(jié)構(gòu),如圖2.1所示

    2024年02月03日
    瀏覽(36)
  • Android布局和控件:創(chuàng)建用戶界面的XML布局文件和常用UI控件詳解

    在Android應(yīng)用開發(fā)中,創(chuàng)建用戶界面是一個重要的任務(wù)。通過使用XML布局文件和常用的UI控件,開發(fā)人員可以設(shè)計和構(gòu)建出吸引人且功能豐富的應(yīng)用界面。本文將詳細介紹如何使用XML布局文件來創(chuàng)建Android應(yīng)用的用戶界面,并深入探討一些常用UI控件的屬性和用法。 XML布局文件是

    2024年02月17日
    瀏覽(29)
  • Android在系統(tǒng)界面上添加窗口

    WindowManager.addView()是Android中的一個方法,用于在屏幕上添加一個窗口。它允許你在應(yīng)用程序的上下文之外創(chuàng)建一個窗口,并將其顯示在其他應(yīng)用程序或系統(tǒng)界面上。 新建一個自定義View用于顯示 使用windowManager.addView()顯示 首先創(chuàng)建了一個WindowManager對象,在這個對象上進行操作

    2024年01月18日
    瀏覽(20)
  • Flutter中為控件添加交互,帶你一起探究Android事件分發(fā)機制

    Flutter中為控件添加交互,帶你一起探究Android事件分發(fā)機制

    ), ); } } 代碼運行效果如圖: 2.父widget管理widget的狀態(tài) 對于父widget來說,管理狀態(tài)并告訴其子widget何時更新通常是最有意義的。 例如,IconButton允許您將圖標視為可點按的按鈕。 IconButton是一個無狀態(tài)的小部件,因為我們認為父widget需要知道該按鈕是否被點擊來采取相應(yīng)的處理

    2024年04月11日
    瀏覽(48)
  • android:登錄界面,輸入框輸入數(shù)量達到了之后自動隱藏鍵盤。

    一、前言: 這篇文章是關(guān)于當(dāng)我們輸入賬號密碼時,達到11位(自定義)時,自動隱藏鍵盤。 二、上代碼: 界面布局: 對應(yīng)的Activity:LoginMainActivity 新建一個工具類:ViewUtil

    2024年02月12日
    瀏覽(16)
  • RK android 9.0 添加sh腳本開機自動運行

    diff --git a/device.mk b/device.mk old mode 100644 new mode 100755 index 4c698b7..c635798 --- a/device.mk +++ b/device.mk @@ -126,6 +126,8 @@ PRODUCT_COPY_FILES += ? ? ?$(call add-to-product-copy-files-if-exists,device/rockchip/common/init.$(TARGET_BOARD_HARDWARE).bootmode.nvme.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/hw/init.$(TARGET_BOARD_HARDWARE).bootm

    2024年02月08日
    瀏覽(20)
  • Android開發(fā)之自定義控件-組合控件的開發(fā)與實現(xiàn)

    Android開發(fā)之自定義控件-組合控件的開發(fā)與實現(xiàn)

    最終實現(xiàn)的效果展示圖: ? 類似支付寶微信,底部分隔線對齊標題效果: ? ? ? 完整渲染顯示效果(包含三個條目右邊不同顏色的文字): 立體效果:? 隱藏資產(chǎn)總額條目右邊更多箭頭? 隱藏中國歷史條目右邊的文字: 隱藏中國歷史條目下邊的分隔線: 隱藏條目2中國歷史左

    2024年02月10日
    瀏覽(23)
  • Android 實現(xiàn)閱讀用戶協(xié)議的文字控件效果

    Android 實現(xiàn)閱讀用戶協(xié)議的文字控件效果

    開發(fā)中,經(jīng)常要用到一些閱讀隱私協(xié)議的場景,原生的textview控件很難做到在一個控件里有兩個點擊事件,那現(xiàn)在就來安利一個強大的組件——SpannableStringBuilder。 先看看效果: ?直接上代碼,布局文件: 布局文件很容易理解,一個checkbox實現(xiàn)選中效果,旁邊加一個textview。

    2024年02月15日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包