2013-06-27 78 views
0

我有一個RelativeLayout activity.xml通過代碼設置元素的佈局利潤率的RelativeLayout

<RelativeLayout 
    android:layout_height="50dip" 
    android:layout_width="fill_parent" 
    android:id="@+id/relativeLayout"   
    android:background="#686868" 
> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_alignParentLeft="true" 
     android:text="Back" 
     android:textColor="#FFFFFF" 
     android:id="@+id/btnBack" 
    /> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/title_header" 
     android:text="Header of page" 
     android:layout_centerInParent="true" 
     android:textColor="#FFFFFF" 
    /> 
</RelativeLayout> 

的代碼這裏是活動的代碼: -

public class MainActivity extends Activity { 
     int valCheck; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity); 
       if(valCheck==0) 
       { 
        //here set title again for backbutton is "backPreviousPage" and set 
        //position again of title_header is right of backButton with 
        //margin=5dip(not set position is centerInParent as code 

       } 


    } 

} 

我不認識路通過代碼獲取元素並在relativeLayout中設置邊距...你能幫我嗎?

回答

0

試試這種方式。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams(); 
params.setMargins(80, 0, 0, 0); //left, top, right, bottom 
params.height = 60; //set height dynamically 
params.width = 200; // set width dynamically 
relativeLayout.setLayoutParams(params); 

我希望這會幫助你。

+0

headView和head_params是哪裏? – anata

+0

檢查編輯答案。 – Gunaseelan

0

要訪問Android代碼中的UI元素,請使用findViewById方法。例如:

Button btnBack = (Button)findViewById(R.id.btnBack); 

一旦你有機會獲得你所需要的,你可以使用API​​提供的setter函數來改變佈局的需要,是元素。由於大多數(如果不是全部的話)UI元素是View類的子類,因此您需要的方法很可能會在那裏定義。這裏有一個很好的參考: http://developer.android.com/reference/android/view/View.html

所有查看對象繼承setLayoutParams方法。因此,您可以將Gunaseelan建議的方法應用於Button和TextView。

0

這可能有助於你

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 

    params.addRule(RelativeLayout.CENTER_IN_PARENT); 
    params.addRule(RelativeLayout.RIGHT_OF, 1001); 
    params.addRule(RelativeLayout.LEFT_OF, 1000); 

這裏ID 1001和1000是我的EditTextid這也動態加入到相對

0

試試這個代碼....

<LinearLayout 
    android:layout_height="50dip" 
    android:layout_width="fill_parent" 
    android:id="@+id/linearLayout"   
    android:background="#686868" 
    android:orientation="horizontal" 
> 
<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_margin="5dip" 
    android:layout_alignParentLeft="true" 
    android:text="Back" 
    android:textColor="#FFFFFF" 
    android:id="@+id/btnBack" 
/> 
<TextView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/title_header" 
    android:text="Header of page" 
    android:layout_centerInParent="true" 
    android:textColor="#FFFFFF" 
/> 
</LinearLayout> 

和java代碼...

public class MainActivity extends Activity { 
    int valCheck; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); 

    Button btn = (Button) findViewById(R.id.btnBack); 
      if(valCheck==0) 
      { 
       //here set title again for backbutton is "backPreviousPage" and set 
       //position again of title_header is right of backButton with 
       //margin=5dip(not set position is centerInParent as code 
       btn.setText("backPreviousPage"); 
      } 
    } 
}