Android程式碼中動態添加布局
動態新增示例一:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.VERTICAL);
Button btn1 = new Button(this);
Button btn2 = new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(btn1);
layout2.addView(btn2);
setContentView(layout2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setTitle("點選button1 ");
Button btn3=new Button(v.getContext());
layout2.addView(btn3);
btn3.setText("Button3");
}
});
}
}
-------------------------------------------------------------------------------------------------------------------------------------
動態新增示例二:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ImageView新增到這個RelativeLayout下邊
RelativeLayout rl_1 = (RelativeLayout)findViewById(R.id.rl_1);
//要被新增的ImageView
ImageView iv_1 = new ImageView(this);
iv_1.setImageResource(R.drawable.ic_launcher);
// iv.setId(110);//注意這點 設定id 特別注意的是設定id(imgApple2.setId(110);),方便在以後onclick監聽事件中進行對應的處理
iv_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you clicked me", 0).show();
}
});
//新增時的佈局引數設定
RelativeLayout.LayoutParams layoutParams_1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams_1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// layoutParams_1.setMargins(30, 50, 100, 100);
layoutParams_1.leftMargin=500;
layoutParams_1.topMargin = 500;
rl_1.addView(iv_1,layoutParams_1);
//ImageView新增到這個RelativeLayout下邊
RelativeLayout rl_2 = (RelativeLayout)findViewById(R.id.rl_2);
//要被新增的ImageView
ImageView iv_2 = new ImageView(this);
iv_2.setImageResource(R.drawable.ic_launcher);
// iv.setId(110);//注意這點 設定id
iv_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you clicked me", 0).show();
}
});
//新增時的佈局引數設定
RelativeLayout.LayoutParams layoutParams_2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams_2.setMargins(30, 50, 100, 100);
layoutParams_2.leftMargin=40;
layoutParams_2.topMargin = 400;
rl_2.addView(iv_2,layoutParams_2);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
動態新增示例三:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設定佈局
setContentView(R.layout.activity_main);
//自定義的帶提示文字的EditText元件
EditText ed = new EditText(this);
ed.setHint("請輸入。。。。。。。。");
//設定從左邊140px處開始輸入文字
ed.setPadding(140, 0, 0, 0);
//向如下relativeLayout這個佈局中動態新增一個EditText
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl_2);
//建立一個LayoutParams物件
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//設定android:layout_below屬性的值
layoutParams.addRule(RelativeLayout.BELOW, R.id.et_inner);
//動態新增EditText
relativeLayout.addView(ed, layoutParams);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
動態新增示例(綜合《很好》):
public class MainActivity extends Activity {
private static int id = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);
LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout newSingleRL=new RelativeLayout(this);
for(int i=0;i<10;)
{
newSingleRL=generateSingleLayout(id,"第"+(++i)+"個動態列表");
lin.addView(newSingleRL,LP_FW);//全部用父結點的佈局引數
}
// final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //獲取總根結點
// setContentView(root); //這裡必須是總根結點
}
/**
* 新建一個列表item
* @param imageID 新建imageView的ID值
* @param str TextView要顯示的文字
* @return 新建的單項佈局變數
*/
private RelativeLayout generateSingleLayout(int imageID,String str)
{
//最外層相對佈局:其他(一個LinearLayout,一個ImageView)都會新增在此佈局內部。
RelativeLayout layout_root_relative=new RelativeLayout(this);
//ImageView被新增在最外層layout_root_relative的右邊:params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView imageView = new ImageView(this);
//out.1
RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setPadding(5, 5, 5, 5);
RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageView.setLayoutParams(RL_WW);
imageView.setClickable(true);
imageView.setId(imageID);
imageView.setImageResource(R.drawable.plus);
layout_root_relative.addView(imageView);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//LinearLayout被新增在最外層layout_root_relative裡邊,並且在ImageView的左邊:params.addRule(RelativeLayout.LEFT_OF,imageID);
LinearLayout layout_sub_Lin=new LinearLayout(this);
layout_sub_Lin.setBackgroundColor(Color.argb(0xff, 0x00, 0xff, 0x00));
layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);
layout_sub_Lin.setPadding(5, 5, 5, 5);
//TextView是被新增在最外層layout_root_relative內部的LinearLayout裡邊
TextView tv = new TextView(this);
//inner.1
LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setText(str);
tv.setTextColor(Color.argb(0xff, 0x00, 0x00, 0x00));
tv.setTextSize(20);
tv.setLayoutParams(LP_WW);
layout_sub_Lin.addView(tv);
//out.2
RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意這個位置,用的是父容器的佈局引數
RL_MW.setMargins(5, 5, 150, 5);
RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);
layout_root_relative.addView(layout_sub_Lin,RL_MW);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
return layout_root_relative;
}
}
--------------------------------------------------------------------------------------------------------------------------------------
動態新增:尺碼,顏色,容量,可以使用這裡(很好)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
// int height = dm.heightPixels;
// 自定義layout元件
RelativeLayout layout = new RelativeLayout(this);
// 這裡建立16個按鈕,每行放置4個按鈕
Button Btn[] = new Button[14];
int j = -1;
for (int i = 0; i < Btn.length; i++) {
Btn[i] = new Button(this);
Btn[i].setId(2000 + i);
Btn[i].setText("按鈕" + i);
RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(
(width - 50) / 4, 100); // 設定按鈕的寬度和高度
if (i % 4 == 0) {
j++;
}
btParams.leftMargin = 10 + ((width - 50) / 4 + 10) * (i % 4); // 橫座標定位
btParams.topMargin = 20 + 100 * j; // 縱座標定位
layout.addView(Btn[i], btParams); // 將按鈕放入layout元件
}
this.setContentView(layout);
// 批量設定監聽
for (int k = 0; k <= Btn.length - 1; k++) {
// 這裡不需要findId,因為建立的時候已經確定哪個按鈕對應哪個Id
Btn[k].setTag(k); // 為按鈕設定一個標記,來確認是按下了哪一個按鈕
Btn[k].setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
int i = (Integer) v.getTag(); // 這裡的i不能在外部定義,因為內部類的關係,內部類好多繁瑣的東西,要好好研究一番
Toast.makeText(MainActivity.this, "count = " + i,
Toast.LENGTH_SHORT).show();
}
});
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
一種思路:
通過使用LayoutInflater 每次點選按鈕時候去讀取佈局檔案,然後找到佈局檔案裡面的各個VIEW 操作完VIEW 後加載進我們setContentView 方面裡面的要放的佈局檔案裡面,
每次動態載入檔案必需 呼叫 removeAllViews方法,清除之前的載入進來的 View 。
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//上邊一個線性佈局,裡邊左右各一個按鈕,。下邊一個線性佈局,當點選上邊的線性佈局裡邊的左右按鈕時,此處的內容改變。
final LayoutInflater inflater = LayoutInflater.from(this);
Button btn = (Button) findViewById(R.id.Button01);
Button btn2 = (Button) findViewById(R.id.Button02);
final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);//下邊的線性佈局
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = View.inflate(MainActivity.this, R.layout.listview, null); //listview.xml裡邊根節點一個線性佈局(感覺有點多餘),線性佈局裡邊一個listview
LinearLayout ll = (LinearLayout) view.findViewById(R.id.layout);
ListView lv = (ListView) ll.getChildAt(0);
lv.setAdapter(new listAdapter(MainActivity.this));
lin.removeAllViews();
lin.addView(ll);
// LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.listview, null).findViewById(R.id.layout);
// ListView lv = (ListView) layout.getChildAt(0);
// lv.setAdapter(new listAdapter(MainActivity.this));
// lin.removeAllViews();
// lin.addView(layout);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.hello, null).findViewById(R.id.hellolayout); //hello.xml裡邊根節點是一個線性佈局(感覺有點多餘),線性佈局裡邊一個textview
TextView lv = (TextView) layout.getChildAt(0);
lv.setTextColor(Color.RED);
lin.removeAllViews();
lin.addView(layout);
}
});
}
}
相關推薦
Android程式碼中動態添加布局
動態新增示例一: public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(s
Android中在activity用程式碼動態添加布局
在activity中或fragment中動態添加布局 效果 專案結構 核心程式碼: MainActivity <span style="font-size:18px;">public class MainActivity extends Activity i
Android動態添加布局LayoutInflater簡單用法
下面是關於 LayoutInflater 簡單用法: 直接看例子,我想在 activity_main 檔案中動態的新增一個佈局;下面是 activity_main 檔案中的程式碼: <RelativeLayout xmlns:android="http://schemas.and
Android破解學習之路(十二)—— GP錄影漢化過程及添加布局
## 前言 最近閒著發慌,想起了很久之前就想漢化的一款錄影APP,APP大小不到1MB,但是好用,本期就給大家帶來漢化的基本步驟以及如何在APP中新增我們漢化的資訊 ## 漢化思路 1. **查詢關鍵字** 關鍵字挺好找的,由於APP本身就是英文,我們找到某個英文單詞進行搜尋即可 2. **找到string.
Android ViewGroup添加布局動畫
轉至:http://www.cnblogs.com/whoislcj/p/5802899.html 前言: 前面幾篇文章介紹了補間動畫、逐幀動畫、屬性動畫,大部分都是針對View來實現的動畫,那麼該如何為了一個ViewGroup新增動畫呢?今天結合自定義ViewGroup來學習一下佈局動畫。本
Android動畫效果之自定義ViewGroup添加布局動畫
前言: 前面幾篇文章介紹了補間動畫、逐幀動畫、屬性動畫,大部分都是針對View來實現的動畫,那麼該如何為了一個ViewGroup新增動畫呢?今天結合自定義ViewGroup來學習一下佈局動畫。本文將通過對自定義圖片選擇控制元件設定動畫為例來學習佈局動畫。 其他幾種動畫效果: 自定義
Celery 分布式任務隊列快速入門 以及在Django中動態添加定時任務
快速入門 change pwa waiting processes 中間件 peid eas 復雜 Celery 分布式任務隊列快速入門 以及在Django中動態添加定時任務 轉自 金角大王 http://www.cnblogs.com/alex3714/articles/
在一個table中動態添加一行或刪除一行
table 添加一行 <table id="selected_user_table" width="100%" > <thead> <tr><th class="center" colspan="2" style="background-color:#
Android 程式碼中動態為RadioGroup新增RadioButton
在程式碼中new出RadioButton,並new出佈局引數LayoutParams,通過layoutParams為radioButton設定margin值,再為radioButton設定其他屬性即可。 RadioButton radioButton = new Radio
Android 程式碼中動態設定字型大小-TextView.SetTextSize()
關鍵程式碼 - setTextSize(TypedValue.COMPLEX_UNIT_PX,15); //22畫素 - setTextSize(TypedValue.COMPLEX_UNIT_SP,15); //22SP - setTextSize(Type
iOS 10 Auto Layout介面自動佈局系列3-使用原生NSLayoutConstraint添加布局約束
本系列的第一篇文章介紹了自動佈局的基本原理,第二篇文章通過一個簡單的例子演示瞭如何使用Xcode的Interface Builder(簡稱IB)以視覺化方式新增約束。本篇為該系列的第三篇文章,主要介紹如何通過寫程式碼來添加布局約束。 說句題外話,通過IB視覺化
Android程式碼中動態設定圖片的大小(自動縮放),位置
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andr
在listview的前面或後面添加布局控制元件
在開發中我們可能會遇到這樣的需求 在listview最下面新增一個按鈕 但是這個按鈕不是一直顯示在螢幕上 它就像listview 最後一個view 根據使用者的移動來顯示 應該怎麼實現呢 這裡要用到一個方法 ListView listView = this.getListVi
Android動態添加和移除布局
dmi ase min pan wid ide state idg system 1 package com.hyang.administrator.studentproject; 2 3 import android.os.Bundle; 4 import a
Hadoop學習筆記—13.分布式集群中節點的動態添加與下架
情況 好的 當前 每次 原因 修改 輸入 task tle 開篇:在本筆記系列的第一篇中,我們介紹了如何搭建偽分布與分布模式的Hadoop集群。現在,我們來了解一下在一個Hadoop分布式集群中,如何動態(不關機且正在運行的情況下)地添加一個Hadoop節點與下架一個Had
Android學習探索之App多渠道打包及動態添加修改資源屬性
Android App 前言: 關於Android渠道打包是一個比較老的話題,今天主要記錄總結一下多渠道打包以及如果動態配置修改一些資源屬性。今天以公司實際需求為例進行演示,由於項目復用很多公共的業務組件,而且業務組件之間的跳轉采用Scheme協議,每個業務組
python解析網頁中js動態添加的內容
pytho log hive .cn article gree html .com .html https://www.cnblogs.com/asmblog/archive/2013/05/07/3063809.html https://www.zhihu.com/q
在Qt項目中添加全局宏變量來達到按方案編譯的目的
方案 定義 這也 文件 win 構建 post gpo cti 遇到一個需求,需要根據不同需要編譯所需程序,本人采用了在QtCreator中建立不同的構建設置,配合宏的方式來實現: 1、在項目-構建設置中,添加構建配置2、在構建步驟裏的qmake中的額外參數一欄填入:"DE
Python 爬取網頁中JavaScript動態添加的內容(二)
python tab sta exe div int rom ava script 使用 selenium + phantomjs 實現 1、準備環境 selenium(一個用於web應用程測試的工具)安裝:pip install seleniumphantomjs(是
android 在java程式碼中動態設定控制元件的位置和設定片大小
需要動態改變佈局裡面控制元件的相對位置 如: 1.改變RelativeLayout佈局裡面某個控制元件的layout_toLeftOf 的屬性 RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)view