从LinearLayout中充满右到左 [英] LinearLayout filled from Right to Left

查看:143
本文介绍了从LinearLayout中充满右到左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个提交按钮向右创建一个输入框。在他们之间,他们要跨越屏幕的宽度。目前,我有:

I want to create an input box with a submit button to the right. Between them they should span the width of the screen. Currently I have:

LinearLayout row= new LinearLayout(context);
row.setOrientation(HORIZONTAL);
row.setGravity(Gravity.RIGHT);
EditText input = new EditText(context);
Button submit = new Button(context);
submit.setText("Submit");
row.addView(submit);
row.addView(input,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
myView.addView(row,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

这导致正确的空间分布:提交按钮占据尽可能多的空间,因为它需要的输入按钮占用剩余的空间,但是他们以错误的方式(提交按钮是在左边,尽管设置重力)。如果我带走的严重性,和反向添加元素的行的顺序,将输入框占据屏幕的整个宽度,并提交按钮是不可见的。我在做什么错了?

This results in the correct distribution of space: The submit button taking up as much space as it needs, the input button taking up the remaining space, however they are the wrong way round (the submit button is on the left, despite setting the gravity). If I take away the gravity, and reverse the order of adding the elements to the row, the input box takes up the whole width of the screen, and the submit button is not visible. What am I doing wrong?

推荐答案

我会说这是更好地使用相对布局,并把输入到左按钮。但如果你真的需要这个线性布局,可以只使用重量参数:

I'd say it is better to use relative layout and place input to left of the button. But if you really need this with Linear layout you can just use weight parameter:

    LinearLayout row= new LinearLayout(context);
    EditText input = new EditText(context);
    Button submit = new Button(context);
    submit.setText("Submit");
    LayoutParams inputParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    inputParams.weight = 1;
    row.addView(input,inputParams);
    LayoutParams buttonParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buttonParams.weight = 0;
    row.addView(submit, buttonParams);

这篇关于从LinearLayout中充满右到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆