先定義出想要的控件,給這個控件里填加相應(yīng)的屬性,然后定義一個布局,把控件添加到布局里面,再把這個布局導(dǎo)入到界面里,代碼如下:

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、順慶ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的順慶網(wǎng)站制作公司
RelativeLayout.LayoutParams
layoutParams
=
new
RelativeLayout.LayoutParams(100,
100);
layoutParams.topMargin=8;
layoutParams.leftMargin=8;
layoutParams.rightMargin=8;
layoutParams.bottomMargin=8;
insertLayout.addView(imgApple2,layoutParams);
可以的,android中使用布局是為了加快開發(fā),最終控件還是通過解析XML后,通過代碼添加的。
具體方法:
例如你的布局是一個Linearlayout linear;上面有一本Button btn1;
要刪除這個btn1要做的就是 linear.removeView( btn1 );
動態(tài)添加也是一樣的:
linear.addView( btn2 );
要注意的是,Button btn2 = new Button( context );這時候控件是沒有大小的,必須設(shè)置控件大小以后添加了才能看到。設(shè)置控件大小的方法是view.setLayoutParams();
希望能夠幫到你。
一、方法
使用getLayoutParams() 和setLayoutParams()方法
二、示例代碼
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) aaa.getLayoutParams();
// 取控件aaa當(dāng)前的布局參數(shù)
linearParams.height = 365; // 當(dāng)控件的高強(qiáng)制設(shè)成365象素
aaa.setLayoutParams(linearParams); // 使設(shè)置好的布局參數(shù)應(yīng)用到控件aaa
三、原理
a)getLayoutParams()和setLayoutParams()都是控件基類view的public方法,在外部也可以直接調(diào)用。
b)由于LayoutParams一般是在加入容器中設(shè)置的,所以容易混淆所指定的布局屬性究竟是保存在容器中,還是控件本身的屬性,答案是控件本身。但是在設(shè)置時還是要注意布局屬性與容器種類密切相關(guān)。
代碼:
public class MyView extends View {
//坐標(biāo)軸原點的位置
private int xPoint=60;
private int yPoint=260;
//刻度長度
private int xScale=8; //8個單位構(gòu)成一個刻度
private int yScale=40;
//x與y坐標(biāo)軸的長度
private int xLength=380;
private int yLength=240;
private int MaxDataSize=xLength/xScale; //橫坐標(biāo) 最多可繪制的點
private ListInteger data=new ArrayListInteger(); //存放 縱坐標(biāo) 所描繪的點
private String[] yLabel=new String[yLength/yScale]; //Y軸的刻度上顯示字的集合
private Handler mh=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0){ //判斷接受消息類型
MyView.this.invalidate(); //刷新View
}
};
};
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
for (int i = 0; i yLabel.length; i++) {
yLabel[i]=(i+1)+"M/s";
}
new Thread(new Runnable() {
@Override
public void run() {
while(true){ //在線程中不斷往集合中增加數(shù)據(jù)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(data.size()MaxDataSize){ //判斷集合的長度是否大于最大繪制長度
data.remove(0); //刪除頭數(shù)據(jù)
}
data.add(new Random().nextInt(5)+1); //生成1-6的隨機(jī)數(shù)
mh.sendEmptyMessage(0); //發(fā)送空消息通知刷新
}
}
}).start();
}
Android動態(tài)改變View控件大小的方法:
1、聲明控件參數(shù)獲取對象 LayoutParams lp;
2、獲取控件參數(shù): lp = 控件id.getLayoutParams();
3、設(shè)置控件參數(shù):如高度。 lp.height -= 10;
4:、使設(shè)置生效:控件id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());
分享名稱:android控件動態(tài),android滾動控件
分享地址:http://chinadenli.net/article46/dsgigeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、虛擬主機(jī)、微信小程序、Google、域名注冊、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)