在android中线性放置元素

在android中线性放置元素,android,android-layout,Android,Android Layout,我对android非常陌生,尝试使用android API水平放置元素,而不使用xml 我想做的是水平放置单选按钮和编辑文本。比如: R-----E R-----E LinearLayout root = new LinearLayout(this); root.setOrientation(LinearLayout.HORIZONTAL); RadioGroup buttonGroup = new RadioGroup(this); buttonGroup.setOrientation(R

我对android非常陌生,尝试使用android API水平放置元素,而不使用xml

我想做的是水平放置单选按钮和编辑文本。比如:

R-----E
R-----E
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.HORIZONTAL);

RadioGroup buttonGroup = new RadioGroup(this);
buttonGroup.setOrientation(RadioGroup.VERTICAL);

LinearLayout editLayout = new LinearLayout(this);
editLayout.setOrientation(LinearLayout.VERTICAL);

//Add left/right pane to root layout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
root.addView(buttonGroup, lp);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
root.addView(editLayout, lp);

//Fixed height for each row item (45dp in this case)
//Getting DP values in Java code is really ugly, which is why we use XML for this stuff
int rowHeightDp = (int)TypedValue.applyDimension(COMPLEX_UNIT_DIP, 45.0f, getResources.getDisplayMetrics());

for(Map.Entry<String,String> entry : storedCards.entrySet())
{

    EditText ed = new EditText(this);
    RadioButton rb  = new RadioButton(this);
    rb.setId(counter++);
    rb.setText(entry.getKey());

    //Add each item with its LayoutParams
    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        rowHeightDp) );
    buttonGroup.addView(rb, lp1);

    lp1 = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        rowHeightDp) );
    editLayout.addView(ed, lp1);
}
我试过这样的代码:

RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
for(Map.Entry<String,String> entry : storedCards.entrySet())
{
    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    EditText ed = new EditText(this);
    RadioButton rb  = new RadioButton(this);
    rb.setId(counter++);

    lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());
    ed.setLayoutParams(lp2);

    rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
    rb.setText(entry.getKey());
    relativeLayout.addView(ed);
}
ER
R
但是我没有得到正确的结果。我得到的只有这样:

RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
for(Map.Entry<String,String> entry : storedCards.entrySet())
{
    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    EditText ed = new EditText(this);
    RadioButton rb  = new RadioButton(this);
    rb.setId(counter++);

    lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());
    ed.setLayoutParams(lp2);

    rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
    rb.setText(entry.getKey());
    relativeLayout.addView(ed);
}
ER
R
所有
EditText
正在重叠。我在哪里犯了错误


提前感谢。

您不能期望
RelativeLayout
相对于它不包含的其他视图放置视图。
RelativeLayout
无法理解
单选按钮的ID,因为它们尚未添加到其中。因此,除了
RadioGroup
(它只是一个
线性布局
)之外,添加到任何其他布局的
RadioButton
)将不会具有用户点击进行检查时可能要查找的互斥逻辑

因此,您必须将您的
单选按钮
项以垂直的
射线组
进行布局,让您的
编辑文本
项在其旁边排列的最简单方法是在其旁边创建第二个
线性布局
,并使用固定高度确保每个“行”匹配。比如:

R-----E
R-----E
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.HORIZONTAL);

RadioGroup buttonGroup = new RadioGroup(this);
buttonGroup.setOrientation(RadioGroup.VERTICAL);

LinearLayout editLayout = new LinearLayout(this);
editLayout.setOrientation(LinearLayout.VERTICAL);

//Add left/right pane to root layout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
root.addView(buttonGroup, lp);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
root.addView(editLayout, lp);

//Fixed height for each row item (45dp in this case)
//Getting DP values in Java code is really ugly, which is why we use XML for this stuff
int rowHeightDp = (int)TypedValue.applyDimension(COMPLEX_UNIT_DIP, 45.0f, getResources.getDisplayMetrics());

for(Map.Entry<String,String> entry : storedCards.entrySet())
{

    EditText ed = new EditText(this);
    RadioButton rb  = new RadioButton(this);
    rb.setId(counter++);
    rb.setText(entry.getKey());

    //Add each item with its LayoutParams
    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        rowHeightDp) );
    buttonGroup.addView(rb, lp1);

    lp1 = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        rowHeightDp) );
    editLayout.addView(ed, lp1);
}
LinearLayout root=新的LinearLayout(此);
根。设置方向(线性布局。水平);
RadioGroup按钮组=新的RadioGroup(此);
按钮组设置方向(放射组垂直);
LinearLayout editLayout=新的LinearLayout(此);
editLayout.setOrientation(LinearLayout.VERTICAL);
//将左/右窗格添加到根布局
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
root.addView(按钮组,lp);
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_父级,
RelativeLayout.LayoutParams.WRAP_内容);
root.addView(编辑布局,lp);
//每行项目的固定高度(本例中为45dp)
//在Java代码中获取DP值真的很难看,这就是为什么我们使用XML来实现这一点
int rowHeightDp=(int)TypedValue.applyDimension(复数单位,45.0f,getResources.getDisplayMetrics());
对于(Map.Entry:storedCards.entrySet())
{
EditText ed=新的EditText(本);
RadioButton rb=新的RadioButton(本);
rb.setId(计数器++);
rb.setText(entry.getKey());
//添加每个项目及其LayoutParams
LinearLayout.LayoutParams lp1=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
rowHeightDp);
buttonGroup.addView(rb,lp1);
lp1=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
rowHeightDp);
editLayout.addView(ed,lp1);
}

这将创建两个并排的布局,由于项目高度固定,它们保持对齐。您可以在用于每行的
LayoutParams
中调整项目高度。您可以看到,在纯Java中进行布局非常冗长,这也是首选XML方法的原因。

谢谢您的回答。。工作得很有魅力!是的,我同意理解java代码是相当糟糕的。但是我想根据
storedCards
上的地图条目生成视图;因此,我想在这种情况下,xml不会帮助我,而是我需要自己编写布局代码!这不是真的,它只是意味着不能在一个XML文件中定义整个布局。您可以在一个布局中定义根和容器,在它们自己的XML文件中定义其他元素,并在Java代码中膨胀/附加。这样布局仍然是动态的,但不必写出所有的布局参数。