日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

动态添加控件及将某XML动态加入到Activity显示

發布時間:2025/6/15 asp.net 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态添加控件及将某XML动态加入到Activity显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、動態添加控件、設置參數

這個難度比較大,放在前面講,用的也比較多,普通情況下,我們會提前把布局XML寫好,然后對XML中的元素進行設置,但這種方法在有些情況下就顯得不適合,比較聊天應用,比如帖子的回復情況。針對這些情況,我們要動態根據獲取到的數據增加控件或控件組的數量,廢話不多說,下面就開整吧,先看個效果圖:

? ? ? ? ? ? ? ? ? ? ? ??????? ? ?原始XML ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????? ?? 動態添加控件后

??? ? ? ? ?

所做的工作:

1、在原有的界面的基礎上添加一個LinearLayout?? layout;參數設置為:layout_width:wrap_content;layout_height:wrap_content;

對應代碼:

[java] view plaincopy
  • LinearLayout?layout?=?new?LinearLayout(this);?//?線性布局方式??
  • layout.setOrientation(LinearLayout.HORIZONTAL);?//??
  • layout.setBackgroundColor(0xff00ffff);??
  • LinearLayout.LayoutParams?LP_MM?=?new?LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT);????
  • layout.setLayoutParams(LP_MM);??
  • 2、添加一個ImageView;參數設置成layout_width:50;layout_height:50;

    [java] view plaincopy
  • ImageView?imageView=?new?ImageView(this);??
  • imageView.setBackgroundResource(R.drawable.maomao);??
  • LinearLayout.LayoutParams?PARA?=?new?LinearLayout.LayoutParams(50,50);??
  • imageView.setLayoutParams(PARA);??
  • layout.addView(imageView);??
  • 3、添加一個TextView;參數設置成layout_width:wrap_content;layout_height:wrap_content;

    對應代碼:

    [java] view plaincopy
  • TextView?tv?=?new?TextView(this);?//?普通聊天對話??
  • tv.setText("我和貓貓是新添加的");??
  • tv.setBackgroundColor(Color.GRAY);??
  • LinearLayout.LayoutParams?LP_WW?=?new?LinearLayout.LayoutParams(??
  • ????????LayoutParams.WRAP_CONTENT,?LayoutParams.WRAP_CONTENT);??
  • tv.setLayoutParams(LP_WW);??
  • layout.addView(tv);??
  • 4、獲取當前布局,即當前main_activity的LinearLayout布局(這里有兩種方法)
    方法一
    :(這種方法不需要:setContentView(R.layout.activity_main);)

    [java] view plaincopy
  • ?//?獲取需要被添加控件的Linear布局(方法一)??
  • final?LayoutInflater?inflater?=?LayoutInflater.from(this);??
  • LinearLayout?lin?=?(LinearLayout)?inflater.inflate(??
  • ????????R.layout.activity_main,?null).findViewById(??
  • ????????R.id.mainLinearLayout);??
  • 方法二:

    [java] view plaincopy
  • //?獲取需要被添加控件的Linear布局(方法二)??
  • setContentView(R.layout.activity_main);??
  • final?LinearLayout?lin?=?(LinearLayout)?findViewById(R.id.mainLinearLayout);??
  • 5、將動態增加的布局添加到當前布局中并顯示;

    [java] view plaincopy
  • lin.addView(layout);??
  • setContentView(lin);??
  • 5、添加對新增的ImageView的單擊消息響應

    [java] view plaincopy
  • //向動態添加的imageView,添加點擊響應??
  • imageView.setOnClickListener(new?OnClickListener()?{??
  • ??????
  • ????@Override??
  • ????public?void?onClick(View?v)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????Toast.makeText(MainActivity.this,?"點擊了圖片",?Toast.LENGTH_SHORT).show();??
  • ????}??
  • });??
  • ?

    全部代碼:

    [java] view plaincopy
  • package?com.example.try_add_combination_ctrl;??
  • /**?
  • ?*?動態增加控件組?
  • ?*?@author?harvic?
  • ?*?@date?2014-1-9?
  • ?*/??
  • import?android.os.Bundle;??
  • import?android.app.Activity;??
  • import?android.graphics.Color;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.view.ViewGroup.LayoutParams;??
  • import?android.widget.ImageView;??
  • import?android.widget.LinearLayout;??
  • import?android.widget.TextView;??
  • import?android.widget.Toast;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ??
  • //??????//?獲取需要被添加控件的Linear布局(方法一)??
  • //??????final?LayoutInflater?inflater?=?LayoutInflater.from(this);??
  • //??????LinearLayout?lin?=?(LinearLayout)?inflater.inflate(??
  • //??????????????R.layout.activity_main,?null).findViewById(??
  • //??????????????R.id.mainLinearLayout);??
  • ??
  • ????????//?獲取需要被添加控件的Linear布局(方法二)??
  • ????????setContentView(R.layout.activity_main);??
  • ????????final?LinearLayout?lin?=?(LinearLayout)?findViewById(R.id.mainLinearLayout);??
  • ??
  • ????????//?添加一個LinearLayout布局,設置成layout_width:wrap_content;layout_height:wrap_content;??
  • ????????LinearLayout?layout?=?new?LinearLayout(this);?//?線性布局方式??
  • ????????layout.setOrientation(LinearLayout.HORIZONTAL);?//??
  • ????????layout.setBackgroundColor(0xff00ffff);??
  • ????????LinearLayout.LayoutParams?LP_MM?=?new?LinearLayout.LayoutParams(??
  • ????????????????LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT);??
  • ????????layout.setLayoutParams(LP_MM);??
  • ??????????
  • ????????//添加一個ImageView,設置成layout_width:50;layout_height:50;??
  • ????????ImageView?imageView?=?new?ImageView(this);??
  • ????????imageView.setBackgroundResource(R.drawable.maomao);??
  • ????????LinearLayout.LayoutParams?PARA?=?new?LinearLayout.LayoutParams(50,?50);//??
  • ????????imageView.setLayoutParams(PARA);??
  • ????????layout.addView(imageView);??
  • ??
  • ????????//添加一個TextView,設置成layout_width:wrap_content;layout_height:wrap_content;??
  • ????????TextView?tv?=?new?TextView(this);?//?普通聊天對話??
  • ????????tv.setText("我和貓貓是新添加的");??
  • ????????tv.setBackgroundColor(Color.GRAY);??
  • ????????LinearLayout.LayoutParams?LP_WW?=?new?LinearLayout.LayoutParams(??
  • ????????????????LayoutParams.WRAP_CONTENT,?LayoutParams.WRAP_CONTENT);??
  • ????????tv.setLayoutParams(LP_WW);??
  • ????????layout.addView(tv);??
  • ??
  • ????????//將動態增加的布局添加到當前布局中;??
  • ????????lin.addView(layout);??
  • ????????setContentView(lin);??
  • ??????????
  • ????????//?向動態添加的imageView,添加點擊響應??
  • ????????imageView.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????//?TODO?Auto-generated?method?stub??
  • ????????????????Toast.makeText(MainActivity.this,?"點擊了圖片",?Toast.LENGTH_SHORT)??
  • ????????????????????????.show();??
  • ????????????}??
  • ????????});??
  • ??????????
  • ????}??
  • ??
  • }??
  • 源碼在文章最底部給出

    二、將某一XML動態加入到當前Activity顯示

    這里就跟上面的不一樣了,上面的是動態生成的控件或控件組,但這里并不是動態生成的,只是將一個寫好的XML在運行時加入到當前Activity的XML中顯示;

    先看XML布局吧

    1、原來的XML(activity_main.xml)

    [html] view plaincopy
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????xmlns:tools="http://schemas.android.com/tools"??
  • ????android:id="@+id/mainLinearLayout"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:orientation="vertical"??
  • ????tools:context=".MainActivity"?>??
  • ????<TextView???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:background="#00ff00"??
  • ????????android:text="我是原生的,下面的布局是添加的"??
  • ????????android:textSize="16sp"?/>??
  • ??????
  • </LinearLayout>??
  • 2、要增加進去的XML(combination_ctrl.xml)

    [html] view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:id="@+id/combineCtrl"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:orientation="horizontal"?>??
  • ??????
  • ????<ImageView?android:id="@+id/img"???
  • ????????android:layout_width="100dip"??
  • ????????android:layout_height="100dip"???
  • ????????android:layout_margin="10.0dip"??
  • ????????android:padding="2.0dip"??
  • ????????android:scaleType="fitXY"/>??
  • ??
  • ????<LinearLayout?android:orientation="vertical"??
  • ????????android:layout_width="wrap_content"???
  • ????????android:layout_height="wrap_content">??
  • ??
  • ????????<TextView?android:id="@+id/name"???
  • ????????????android:layout_width="wrap_content"??
  • ????????????android:layout_height="wrap_content"???
  • ????????????android:textColor="#FFFFFF00"??
  • ????????????android:textSize="22px"?/>??
  • ????????<TextView?android:id="@+id/info"???
  • ????????????android:layout_width="wrap_content"??
  • ????????????android:layout_height="wrap_content"???
  • ????????????android:textColor="#FF00FFFF"??
  • ????????????android:textSize="13px"?/>??
  • ????</LinearLayout>??
  • ??????
  • ??
  • </LinearLayout>??
  • 看看效果:

    ???????????????????????????? 原形狀態?????????????????????????????????????????????? ??????????????? ? 增加進去后

    ?

    ?全部代碼

    ?

    [java] view plaincopy
  • package?com.example.try_add_layout_from_xml;??
  • ??
  • /**?
  • ?*?將一個現有的XML代碼加入到當前的XML中,但由于ID是一定的,所以與在代碼中添加include效果一樣?
  • ?*?@author?harvic?
  • ?*?@date?2014-1-9?
  • ?*/??
  • import?android.os.Bundle;??
  • import?android.app.Activity;??
  • import?android.view.LayoutInflater;??
  • import?android.view.Menu;??
  • import?android.widget.ImageView;??
  • import?android.widget.LinearLayout;??
  • import?android.widget.TextView;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ??
  • ????????setContentView(R.layout.activity_main);??
  • ????????final?LayoutInflater?inflater?=?LayoutInflater.from(this);??
  • ????????//?獲取需要被添加控件的布局??
  • ????????final?LinearLayout?lin?=?(LinearLayout)?findViewById(R.id.mainLinearLayout);??
  • ????????//?獲取需要添加的布局??
  • ????????LinearLayout?layout?=?(LinearLayout)?inflater.inflate(??
  • ????????????????R.layout.combination_ctrl,?null).findViewById(R.id.combineCtrl);??
  • ????????//?將布局加入到當前布局中??
  • ????????lin.addView(layout);??
  • ??
  • ????????ImageView?imageView?=?(ImageView)?findViewById(R.id.img);??
  • ????????imageView.setBackgroundResource(R.drawable.maomao);??
  • ????????TextView?TV_info?=?(TextView)?findViewById(R.id.info);??
  • ????????TV_info.setText("第一個INOF");??
  • ????????TextView?TV_name?=?(TextView)?findViewById(R.id.name);??
  • ????????TV_name.setText("第一個NAME");??
  • ??
  • ????}??
  • }??
  • 可見代碼非常短,而且關鍵代碼就只有五句,專門列出來

    [java] view plaincopy
  • setContentView(R.layout.activity_main);??
  • final?LayoutInflater?inflater?=?LayoutInflater.from(this);??
  • //?獲取需要被添加控件的布局??
  • final?LinearLayout?lin?=?(LinearLayout)?findViewById(R.id.mainLinearLayout);??
  • //?獲取需要添加的布局??
  • LinearLayout?layout?=?(LinearLayout)?inflater.inflate(??
  • ????????R.layout.combination_ctrl,?null).findViewById(R.id.combineCtrl);??
  • //?將布局加入到當前布局中??
  • lin.addView(layout);??
  • 源碼在文章最底部

    三、相關代碼設置參數匯總

    1、設置margin

    [java] view plaincopy
  • LinearLayout.LayoutParams?lp?=?new?LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,?LinearLayout.LayoutParams.WRAP_CONTENT);??
  • lp.setMargins(10,?20,?30,?40);??
  • imageView.setLayoutParams(lp);??
  • 2、設置layout_weight:

    [java] view plaincopy
  • setLayoutParams(new?LinearLayout.LayoutParams(??
  • ??????????????????????????LinearLayout.LayoutParams.FILL_PARENT,??
  • ??????????????????????????LinearLayout.LayoutParams.FILL_PARENT,weight??
  • ??????????????????????));??
  • 例如:

    [java] view plaincopy
  • TextView?tv_like?=?new?TextView(this);??
  • LinearLayout.LayoutParams?LP_LIKE_MW?=?new?LinearLayout.LayoutParams(??
  • ????????LinearLayout.LayoutParams.MATCH_PARENT,?LinearLayout.LayoutParams.WRAP_CONTENT,?1.0f);??
  • tv_like.setGravity(Gravity.CENTER);??
  • tv_like.setPadding(0,?8,?0,?8);??
  • tv_like.setText("贊(8)");??
  • tv_like.setTextSize(16);??????
  • layout_sub_Lin.addView(tv_like,?LP_LIKE_MW);??


  • ?

    ?


    上源碼(兩個例子代碼混合在一起):http://download.csdn.net/detail/harvic880925/6829633?(不要分,僅供分享)

    ?

    ?請大家尊重原創者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/18042183? 謝謝!!!

    總結

    以上是生活随笔為你收集整理的动态添加控件及将某XML动态加入到Activity显示的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。