简体   繁体   English

如何在RelativeLayout中正确对齐?

[英]How to align in RelativeLayout properly?

Hi I am trying to design a layout programatically where I have a textview and a edittext and a button underneath. 嗨,我正在尝试以编程方式设计一个布局,在其中我有一个textview和一个edittext以及一个按钮。 Two rows. 两行。 First row with TextView. TextView的第一行。 Second row with Edittext and button. 第二行带有Edittext和按钮。 Edittext should extend from left to the button. Edittext应该从左延伸到按钮。 Right now, the Textview appears in the first row, but the second row (with Edittext and Button) is messed. 现在,Textview出现在第一行中,但是第二行(带有Edittext和Button)被弄乱了。 Right now, the EditText doesnt extend all the way to the send button. 现在,EditText并没有一直扩展到发送按钮。 I tried using Wrap_Content, Fill_parent, Match_Parent but no luck. 我尝试使用Wrap_Content,Fill_parent,Match_Parent,但没有运气。 Here is what I have so far: 这是我到目前为止的内容:

   RelativeLayout  layout = new RelativeLayout(this);
   EditText myText = new EditText(this);
   Button myBtn = new Button(this);
   parameters_layout = new
             RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
   myText.setId(1);
   myText.setLayoutParams(parameters_layout);
   layout.addView(myText,parameters_layout);

   parameters_layout =  new 
               RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
               RelativeLayout.LayoutParams.WRAP_CONTENT);
   parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);    
   parameters_layout.addRule(RelativeLayout.RIGHT_OF, myText.getId());
   myBtn.setLayoutParams(parameters_layout);
   layout.addView(myBtn,parameters_layout);

Thank you any help is appreciated. 谢谢您的帮助,不胜感激。

EDIT: Clarification I know 编辑:澄清我知道

          parameters_layout =  new 
           RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
           RelativeLayout.LayoutParams.WRAP_CONTENT);

can be replaced with numbers. 可以用数字代替。 For example: 例如:

          parameters_layout =  new 
           RelativeLayout.LayoutParams(350, 
           RelativeLayout.LayoutParams.WRAP_CONTENT);

Is that 350 just pixels, dp, sp? 那是350个像素,dp,sp吗? What is it? 它是什么?

What I understand is what you want to implement is one Button on the bottom-right corner of the screen and one extended EditText just left of the button. 我了解的是您要实现的是屏幕右下角的一个Button和按钮左侧的一个扩展EditText。 Am I correct? 我对么?

If that, try this. 如果那样,请尝试此。 I tested this code in on my galaxy nexus and it worked fine. 我在我的银河系关系中测试了此代码,并且效果很好。

RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
myBtn.setId(1);

RelativeLayout.LayoutParams parameters_layout = new
    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.LEFT_OF, myBtn.getId());

myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);

parameters_layout =  new   
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
myBtn.setLayoutParams(parameters_layout);

layout.addView(myBtn,parameters_layout);

One tip is when you use RelativeLayout, first locate the view that the position is fixed (in your case bottom-right cornered button) then locate other views by using relative layout specific parameters. 一个技巧是,当您使用RelativeLayout时,首先找到位置固定的视图(在您的情况下为右下角按钮),然后使用特定于布局的相对参数找到其他视图。 (In your case LEFT_OF). (在您的情况下为LEFT_OF)。

Another tip is whenever if you need to assign a ID value in programmatically, don't assign any specific value (in your case '1'), instead of it you can define ID as a resource, and then use it w/o any worries about id conflict or some strange behaviors. 另一个提示是,只要您需要以编程方式分配ID值,就不要分配任何特定值(在您的情况下为'1'),而是可以将ID定义为资源,然后不使用任何值来使用它担心ID冲突或某些奇怪的行为。

There is a document about ID resource, so check it - http://developer.android.com/guide/topics/resources/more-resources.html#Id 有一个有关ID资源的文档,请检查一下-http://developer.android.com/guide/topics/resources/more-resources.html#Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM