我在android中为relativelayout编写了一些代码,但是没有xml,以下是我使用的代码。
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按钮在右侧对齐,但它在左侧对齐并覆盖保存按钮......但是我的代码中有什么问题。
答案 0 :(得分:1)
尝试cancleparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 这应该有用。