【问题标题】:Placing elements linearly in android在android中线性放置元素
【发布时间】:2013-02-05 02:30:55
【问题描述】:

我对 android 非常陌生,并尝试使用不带 xml 的 android api 水平放置元素。

我想要做的是水平放置 RadioButtons 和 EditText。比如:

R-----E
R-----E

我试过这样的代码:

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);
}

这不起作用。但这就是我想要做的:首先,我使用counter 变量为每个单选按钮设置id,并尝试在该单选按钮的右侧站点上设置edittext视图:

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

但我没有得到正确的结果。我得到的只是这样:

ER
R

所有EditText 都被重叠了。我在哪里犯错了?

提前致谢。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您不能指望RelativeLayout 将视图相对于它不包含的其他视图放置。 RelativeLayout 不理解 RadioButtons 的 ID,因为它们尚未添加到其中。因此,添加到除RadioGroup(只是LinearLayout)之外的任何其他布局的RadioButton 将不会具有您在用户点击它们进行检查时可能正在寻找的互斥逻辑。

    因此,您必须将您的 RadioButton 项目放置在垂直的 RadioGroup 中,让您的 EditText 项目在它们旁边排列的最简单方法是在它旁边创建第二个 LinearLayout,并使用固定高度以确保每个“行”匹配。比如:

    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);
    }
    

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

    【讨论】:

    • 感谢您的回答.. 工作就像一个魅力!是的,我同意理解Java代码非常糟糕。但我想根据storedCards 上的地图条目生成视图;所以我想在这种情况下 xml 不会帮助我,而是我需要自己编写布局!
    • 这不是真的,这只是意味着您不能在单个 XML 文件中定义整个布局。您可以在一个布局中定义根和容器,在它们自己的 XML 文件中定义其他元素,并在 Java 代码中进行膨胀/附加。这样布局仍然是动态的,但你不必写出所有的 LayoutParams。
    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多