當前位置:首頁 » 安卓系統 » androidview動態添加

androidview動態添加

發布時間: 2024-10-20 03:00:39

❶ 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中如何使幾個view動起來隨機交換位置呢

這是做的一個部分功能,因為你這個要求起來還是挺難控制演算法的。 view的點擊事件可以使用OnTouch事件做。 實現功能: 繪制三個圖片,移動位置,紅藍移動一次,要全部的話,演算法要很精妙,時間控制也很難,我這個就不說了,我的演算法很菜。希望能給你點啟發。

開始圖:

❸ android viewpager怎麼動態載入view而且view的個數是不一定的。

在自定義適配器里寫一個方法add方法,載入View的時候,增加到適配器中
適配器需要重寫
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
增加數據到適配器後需要調用adapter.notifyDataSetChanged();

❹ android開發 如何在相對布局中動態添加控制項

首先setMargin方法不是RelativeLayout的方法,而是RelativeLayout.LayoutParams的方法。
你應該這麼用:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
TextView mView = new TextView(this);
mView.setId(2);
mView.setText("this is a test text!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
-2, -2);
// layoutParams.setMargins(100, 100, 100, 100);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
layout.addView(mView, layoutParams);
上例是將一個TextView添加到RelativeLayout的底部。你可以把注釋行取消掉,把下一行注釋,再看下效果。

熱點內容
如何使伺服器自動運行 發布:2024-10-20 05:32:15 瀏覽:775
我的世界純生存伺服器地址117 發布:2024-10-20 05:25:14 瀏覽:414
編譯器語法分析器 發布:2024-10-20 05:08:31 瀏覽:799
javajdbc資料庫連接 發布:2024-10-20 04:50:02 瀏覽:10
android獲取子控制項 發布:2024-10-20 04:11:13 瀏覽:242
java介面的作用和意義 發布:2024-10-20 04:07:46 瀏覽:172
安卓如何用耳機內錄 發布:2024-10-20 04:01:31 瀏覽:243
asp無組件上傳進度 發布:2024-10-20 03:59:59 瀏覽:110
伺服器依據埠搭建多個網站 發布:2024-10-20 03:56:08 瀏覽:749
o圈壓縮量 發布:2024-10-20 03:37:00 瀏覽:858