2012-01-24 17 views
0

我是android新手,我想檢索XML文件中的按鈕信息。 (特別是margin_top和margin_left)。在那之後,我想動態地給這個特定的按鈕添加另一個邊距。我可以創建新的按鈕與新的保證金,但我不能檢索當前的按鈕邊距..如何檢索xml中的按鈕信息

任何幫助,將不勝感激。

android.view.ViewGroup.LayoutParams lay = btn1.getLayoutParams(); 

這個外行對象持有的所有信息,但我不能從那個利潤。

我使用下面的代碼創建了我的按鈕。

RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeTest); 
     Button btn3 = new Button(TestActivity.this); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     params.setMargins(200, 200, 200, 200); 
     layout.addView(btn3, params); 
+0

查看我的更新回答 –

回答

1

FlaMM3R

我的佈局看起來像下面,你可以根據自己的佈局,使用同樣的想法。

XML文件中的代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"    
     android:text="@string/hello" 
     android:id="@+id/tv" /> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <Button android:id="@+id/btn" 
      android:layout_width="100dip" 
      android:layout_height="100dip" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="40dp" 
      android:layout_marginTop="36dp" 
      android:text="Button" />  
     <Button android:id="@+id/btn2" 
      android:layout_width="100dip" 
      android:layout_height="100dip" 
      android:layout_marginLeft="148dp" 
      android:layout_marginTop="62dp" 
      android:text="Button" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true"/> 
    </RelativeLayout> 
</LinearLayout> 

的Java文件

public class HelloActivity extends Activity { 
    TextView tv; 
    Button btn; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv =(TextView)findViewById(R.id.tv); 
     btn = (Button)findViewById(R.id.btn);   
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) btn.getLayoutParams();  
     tv.setText("left : "+params.leftMargin+"\ntop : "+params.topMargin); 
     params.setMargins(10, 50, 0, 0); // setting new margins for btn 
    } 
} 

使用相同的想法,讓我知道發生了什麼。我希望這將是你正在尋找的答案。

+0

謝謝巴布,我得到了leftMargin。但價值是不同的。(不是在xml相同的值) – FlaMM3R

+0

是的,但即時通訊使用相對<:ID = 「@ + ID/relativeLayout1」 機器人:layout_width = 「match_parent」 機器人:layout_height = 「WRAP_CONTENT」 的RelativeLayout 機器人> <按鈕 機器人:線性佈局 – FlaMM3R

+0

內部佈局ID =「@ + ID/btn1「 android:layout_width =」100dip「 android:layout_height =」100dip「 android:layout_alignParentLeft =」true「 android:layout_alignParentTop =」true「 androi d:layout_marginLeft = 「40dp」 機器人:layout_marginTop = 「36dp」 機器人:文本= 「按鈕」/> – FlaMM3R

1

舉一個ID在XML文件中的按鈕,使用findViewById像你一樣的RelativeLayout的得到那個按鈕。然後你可以獲得關於該按鈕的所有信息。您也可以修改按鈕的屬性。嘗試一下。

+0

我也試過。在上面的代碼中,btn1是由findViewById檢索的按鈕對象,lay包含有關按鈕的所有信息。 ( – FlaMM3R

+0

檢查我的新答案 –