導航:首頁 > 操作系統 > androidinflatexml

androidinflatexml

發布時間:2025-03-12 11:05:56

android 如何動態布局自定義view,不用XML.

可以直接new View來得到View對象來實現代碼布局。以下為示例代碼:
1.絕對布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
Button btn1 = new Button(this);
btn1.setText(」this is a button」);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1);

2.相對布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
AbsoluteLayout abslayout=new AbsoluteLayout (this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

3.線性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);
//動態添加布局的方法1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); //這樣 main2 作為 main1的子布局 加到了 main1的 根節點下
//動態添加布局的方法2 addView. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null); ll.addView(ll2);

Ⅱ android 怎麼把倆個xml文件整合到一起

private ViewGroup group;
private ViewGroup main;
main = (ViewGroup) inflater.inflate(R.layout.main, null);//這里的R。latout.main就是當前布局
group = (ViewGroup) main.findViewById(R.id.viewGroup);
group.addView(imageViews[i]);//這樣就把一個imageview(你可添加一個任意復雜的View,這個View實例化的時候指向你所謂的其他布局文件)加入當前布局中的某個ViewGroup里了,你布局時可定義一個LinearLayout id 設為(R.id.viewGroup),oratation設為垂直,放到下面就是了。

閱讀全文

與androidinflatexml相關的資料

熱點內容
單片機測量 瀏覽:303
deboor演算法 瀏覽:839
linuxftp550 瀏覽:635
命令行修改文件名 瀏覽:591
安卓系統408如何關閉啟停 瀏覽:696
怎麼才能讓安卓手機崩潰 瀏覽:149
連接雲伺服器今日價格 瀏覽:117
百靈之音加密圖片 瀏覽:624
javalinkedlist刪除 瀏覽:539
用紙做無限拉伸解壓彈簧 瀏覽:979
紅塔銀行app流水怎麼導出 瀏覽:377
javascriptphpjson 瀏覽:114
蘋果轉安卓為什麼花錢 瀏覽:386
深圳好的程序員培訓機構 瀏覽:925
nmap命令解說 瀏覽:347
雲伺服器怎麼能關掉 瀏覽:760
美團app如何下預定單 瀏覽:620
語法新思維pdf 瀏覽:145
為什麼雲伺服器桌面太卡 瀏覽:737
程序員第一年感覺什麼都不會 瀏覽:11