java 如何以编程方式将 Android TextView 与另一个 TextView 的右侧对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12238164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:08:49  来源:igfitidea点击:

How to programmatically align an Android TextView to the right of another TextView

javaandroidandroid-layouttextview

提问by Maverick Snyder

In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.

在下面的代码中,我创建了两个文本视图并以编程方式将它们添加到相对布局中。我想将它们并排对齐。

The code runs fine but is not placing the new TextViewto the right of previous TextViewinstead the new TextViewis positioned at margin (0,0,0,0) i.e. upper right corner of the screen:

代码运行良好,但没有将新放置TextView在前一个右侧,TextView而是将新放置TextView在边距 (0,0,0,0) 处,即屏幕的右上角:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);

    textView[0] = new TextView(this);//creates first textview
    textView[0].setId(0);
    textView[0].setText("1");
    textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml

    relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    textView[0].setLayoutParams(relativeLayoutParams);

    relativeLayout.addView(textView[0]);//creates another textview

    textView[1] = new TextView(this);

    textView[1].setBackgroundResource(R.drawable.shape);

    RelativeLayout.LayoutParams relativeLayoutParams=
            new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview

    relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
    textView[1].setText("2");

    relativeLayout.addView(textView[1], relativeLayoutParams);

回答by msrxthr

Try the following:

请尝试以下操作:

  1. Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer)
  2. Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOP
  1. 将 textView[0] 的 id 设置为 1 而不是 0(id 需要为正整数)
  2. 在 textView[1] 的 relativeLayoutParams 中添加一个RelativeLayout.ALIGN_TOP的规则

The following worked for me:

以下对我有用:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;       
TextView[] textView = new TextView[2];

// 1st TextView
textView[0] = new TextView(this);

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

textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");   

relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

relativeLayout.addView(textView[0], relativeLayoutParams);

// 2nd TextView
textView[1] = new TextView(this);

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

textView[1].setText("2");

relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
        textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
        textView[0].getId()); // added top alignment rule

relativeLayout.addView(textView[1], relativeLayoutParams);

回答by Marc Van Daele

Just a guess: can you try to use a different id than 0 ?

只是猜测:您可以尝试使用与 0 不同的 id 吗?