2011-09-07 51 views
1

Output of my code.我在android中編寫了一些代碼,但沒有使用xml,下面是我使用的代碼。RelativeLayout通過代碼

package layout.program; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class Relativelayout extends Activity{ 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.relativelayout); 

    RelativeLayout relativelayout=new RelativeLayout(this); 
    RelativeLayout.LayoutParams layoutparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    TextView textview=new TextView(this); 
    textview.setText("TopLeft"); 
    textview.setId(1); 
    RelativeLayout.LayoutParams textviewparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    textviewparams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    textviewparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    relativelayout.addView(textview, textviewparams); 

    TextView textview2=new TextView(this); 
    textview2.setText("TopRight"); 
    RelativeLayout.LayoutParams textviewparams2=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    textviewparams2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    textviewparams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    relativelayout.addView(textview2, textviewparams2); 

    TextView textview3=new TextView(this); 
    textview3.setText("BottomRight"); 
    RelativeLayout.LayoutParams textviewparams3=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    textviewparams3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    textviewparams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    relativelayout.addView(textview3, textviewparams3); 

    TextView textview4=new TextView(this); 
    textview4.setText("BottomLeft"); 
    RelativeLayout.LayoutParams textviewparams4=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    textviewparams4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    textviewparams4.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    relativelayout.addView(textview4, textviewparams4); 

    TextView textview5=new TextView(this); 
    textview5.setText("center"); 
    RelativeLayout.LayoutParams textviewparams5=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    textviewparams5.addRule(RelativeLayout.CENTER_IN_PARENT); 
    relativelayout.addView(textview5, textviewparams5); 

    EditText edittext=new EditText(this); 
    edittext.setHeight(90); 
    edittext.setTextSize(18); 
    edittext.setId(2); 
    RelativeLayout.LayoutParams edittextparams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    edittextparams.addRule(RelativeLayout.ALIGN_LEFT,1); 
    edittextparams.addRule(RelativeLayout.BELOW,1); 
    relativelayout.addView(edittext,edittextparams); 

    Button save=new Button(this); 
    save.setText("SAVE"); 
    save.setId(3); 
    RelativeLayout.LayoutParams saveparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    saveparams.addRule(RelativeLayout.BELOW,2); 
    saveparams.addRule(RelativeLayout.ALIGN_LEFT); 
    relativelayout.addView(save,saveparams); 

    Button cancle=new Button(this); 
    cancle.setText("CANCLE"); 
    RelativeLayout.LayoutParams cancleparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    cancleparams.addRule(RelativeLayout.BELOW,2); 
    cancleparams.addRule(RelativeLayout.ALIGN_RIGHT); 
    relativelayout.addView(cancle,cancleparams); 

    this.addContentView(relativelayout, layoutparams); 
    } 

}

問題是我希望CANCLE按鈕上側的權利,但其在左邊對齊並覆蓋保存按鈕對準......但什麼是我的代碼問題,

回答

1

嘗試cancleparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 這應該工作。

+0

是的......它的工作非常感謝.. –