導航:首頁 > 操作系統 > 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相關的資料

熱點內容
程序員放棄後會怎樣 瀏覽:160
河北模具編程 瀏覽:178
adb查找命令 瀏覽:309
安卓手機視頻文件夾怎麼打開 瀏覽:303
平板加密手機後怎麼關閉 瀏覽:557
流媒體伺服器應該注意什麼 瀏覽:528
d8命令編譯 瀏覽:942
壓縮包解壓需要多少空間 瀏覽:139
如何查找app屬性 瀏覽:380
android人臉識別技術 瀏覽:305
pc104編程 瀏覽:329
二維碼反編譯破解推廣 瀏覽:674
修改伺服器的mac地址 瀏覽:521
好玩的編程軟體 瀏覽:892
編程語言創始人有錢嗎 瀏覽:797
短視頻app怎麼獲客 瀏覽:8
查看雲伺服器的應用 瀏覽:427
javadump工具 瀏覽:558
程序員16g 瀏覽:421
程序員沒有辦法成為top怎麼辦 瀏覽:196