2010-08-09 108 views
19

如果您使用AbsoluteLayout(我知道它已被棄用,但它是解決我的 problem的唯一方法),您可以給childViews標記android:layout_xandroid:layout_y以設置它們的絕對在AbsoluteLayout內的位置。Android:以編程方式更改視圖的絕對位置

但是我不想在xml中設置這些信息,因爲我只在運行時就知道它們。那麼我怎樣才能以編程方式在運行時設置這些參數?我在視圖上看不到任何方法,如view.setLayoutX(int x)或其他。

這裏是我的XML,當我設置layout_xlayout_y值的正常工作,:

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
    android:src="@drawable/myImageView" 
    android:layout_width="1298px" 
    android:layout_height="945px" 
    android:layout_x="0px" 
    android:layout_y="0px" /> 
<Button 
    android:id="@+id/myButton1" 
    android:text="23" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_x="50px" 
    android:layout_y="300px" 
    android:tag="23"/> 


<Button 
    android:id="@+id/myButton2" 
    android:text="48" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_x="50px" 
    android:layout_y="300px" 
    android:tag="48"/> 

</AbsoluteLayout> 

其實,我並不想設置的XML內的任何按鈕了,而是一些檢索通過遠程信息並根據該信息添加按鈕。

這裏是部分我用這樣在我onCreateMethod代碼添加這些按鈕:

for (MyRemoteObject remoteObject: list) { 
    Button button = new Button(this); 
    button.setOnClickListener (listener); 
    button.setTag(remoteObject.id); 
    button.setText(remoteObject.id); 
    // button.setLayoutX(remoteObject.x) ???? 
    // button.setLayoutY(remoteObject.y) ???? 
    myLayout.addView(button); 
    } 

回答

12

使用addView的版本需要LayoutParams

LayoutParams params = mLayout.generateLayoutParams(); 
params.x = remoteObject.x; 
params.y = remoteObject.y; 
mLayout.addView(button, params); 
+1

謝謝你,但如何如何創建的LayoutParams因爲mLayout是類型AbsoluteLayout的? AbsoluteLayout只有一個方法absolutLayout.generatelayoutParams(AttributeSet)。我不知道如何創建一個attributeSet在第一個地方,以便我可以將該設置傳遞給generateLayoutParams() - 方法 – 2010-08-09 17:35:46

+0

我有同樣的問題與RelativeLayout而不是絕對 – Amplify91 2011-03-25 20:04:10

9

針對您的對Mayra的回答發表評論,我有一個和RelativeLayout相似的問題,而不是AbsoluteLayout。解決方案是使用類似的方法並將其轉換爲佈局類型。這樣的事情:

LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams(); 
params.height = 360; 
params.width = 360; 
params.addRule(CENTER_IN_PARENT); 
this.addView(board, params); 

我花了我一個腦筋想出來,所以我想在這裏發佈給別人,如果他們需要它。

4

我只是有同樣的問題,但發現了一個不同的解決方案。

int width = 100, height = 50, x = 10, y = 20; 
Button button = new Button(this); 
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout); 
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y)); 

如果你找到了需要使用什麼「FILL_PARENT」(暗示嘗試-1),那麼你可以使用這些常量的寬度和高度。

0

我不知道爲什麼,但是當移動頂部和左側邊緣時,Android會將右邊緣和底部邊緣保持在相同的位置。因爲我無法改變的寬度和高度正確(圖像正在消失),我用this

LayoutParams layoutParams=new LayoutParams(width, height); 
layoutParams.leftMargin = newLeft; 
layoutParams.topMargin = newTop; 
imageView.setLayoutParams(layoutParams); 

我不知道這是否是最好的方式,但對我的工作。我已經在2.2+中測試過,並且工作正常!

您必須導入android.widget.LinearLayout.LayoutParams;因爲默認的是一個ViewGroup的的LayoutParams(由Davit T

0

上述答案right.but這個人會更完美

AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location); 
layout.addView(view,layoutParams) 
相關問題