gpt4 book ai didi

android - RelativeLayout 通过代码

转载 作者:行者123 更新时间:2023-11-30 04:33:15 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com