android - RelativeLayout 通过代码

标签 android android-layout

Output of my code.我在 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);
  } 

问题是我希望取消按钮在右侧对齐,但它在左侧对齐并覆盖保存按钮...但是我的代码中有什么问题。

最佳答案

试试 cancleparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 这应该有效。

关于android - RelativeLayout 通过代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329192/

相关文章:

java - 使用 ShareActionProvider 共享内容时检测成功/失败

android - 实现 OnClickListener 的正确方法是什么

android - 添加到 android 后无法从数据库中看到更改?

android - 如何清除 TableLayout 中的所有行?

android - 如何在 recyclerview 之间放置广告

android - 如何在 android 中获取 MapView API?

android - TextView lineSpacingMultiplier 小于零会截断最后一行

android - 缩小屏幕上的对话框

android - 在 Android 中访问子文件夹中的 xml 资源

android - RelativeLayout中如何引用TextView