2012-02-04 88 views
3

我使用adt插件在eclipse中製作測試應用程序。我想,當一個按鈕被點擊,將其移動在屏幕的隨機位置,我發現了一些例如周圍不受任何政府上網工程如何以絕對佈局以編程方式移動按鈕

Button noBtn = (Button) findViewById(R.id.NoBtn); 
noBtn.setOnClickListener(new OnClickListener() { 
public void onClick(View v) 
{ 
    Button noBtn = (Button) findViewById(R.id.NoBtn); 
    AbsoluteLayout.LayoutParams OBJ=new 
    AbsoluteLayout.LayoutParams(35,35,408,160); 
    noBtn.setLayoutParams(OBJ); 
} 
}); 

,我通過使用機器人的layoutparam得到應用程序錯誤

+0

那麼,什麼是錯誤? – Squonk 2012-02-04 21:33:44

+0

我在xml中使用了RlativeLayout。所以我改變它'RelativeLayout.LayoutParams OBJ = newRelativeLayout.LayoutParams(50,80); OBJ.topMargin = randomHeight; int a = ApplicationUtils.getScreenHeight(getWindowManager()); int aa = ApplicationUtils.getScreenWidth(getWindowManager()); OBJ.leftMargin = randomWidth; noBtn.setLayoutParams(OBJ); – ddarellis 2012-02-05 09:38:25

回答

4

,你可以通過java代碼指定你的小部件的位置,而不是在android的xml文件中指定它。

AbsoluteLayout.LayoutParams OBJ = new AbsoluteLayout.LayoutParams(35,35,408,160); 
button.setLayoutParams(OBJ); 

這裏 -

AbsoluteLayout.LayoutParams(width,height,X-position,Y-position) 

就是我被指定爲(35,35,408,160)