Android RelativeLayout ALIGN_左不工作
我有一个按钮和一个文本视图。我试图将按钮一直对齐到屏幕的右侧,然后在其左侧放置一个文本视图,但它不起作用。下面的代码将按钮放置在正确的位置,一直向右,但是当文本视图放到屏幕上时,它会将按钮从屏幕上敲下来,并在某种程度上替换了按钮所在的文本视图。我不明白它为什么这么做Android RelativeLayout ALIGN_左不工作,android,android-relativelayout,Android,Android Relativelayout,我有一个按钮和一个文本视图。我试图将按钮一直对齐到屏幕的右侧,然后在其左侧放置一个文本视图,但它不起作用。下面的代码将按钮放置在正确的位置,一直向右,但是当文本视图放到屏幕上时,它会将按钮从屏幕上敲下来,并在某种程度上替换了按钮所在的文本视图。我不明白它为什么这么做 RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout); Button button = new Button(this); button.set
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
Button button = new Button(this);
button.setId(12345);
RelativeLayout.LayoutParams layoutForAlignment = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignment.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(button, layoutForAlignment);
TextView myTextView = new TextView(getApplicationContext());
myTextView.setText("Testing");
RelativeLayout.LayoutParams layoutForAlignmentX = (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
layout.addView(myTextView, layoutForAlignmentX);
我认为你的问题在于这一行:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, myTextView.getId());
应该是:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
这样,您就可以在按钮的左侧设置文本视图。我认为您的问题在于这一行:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, myTextView.getId());
应该是:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
通过这种方式,您可以在按钮的左侧设置textview。我认为您应该为textview创建一个新的RelativeLayoutParams,因为您将获得一个参数,其规则是对齐其父项的右侧(按钮的参数) 您还必须为按钮提供一个id 你可以:
Button button = new Button(this);
upgradeButton.setId(12345);
您应该为按钮提供id:
Button button = new Button(this);
button.setId(12345);
证明此代码:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
Button button = new Button(this);
button.setId(12345);
RelativeLayout.LayoutParams layoutForAlignment = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignment.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(button, layoutForAlignment);
TextView myTextView = new TextView(getApplicationContext());
myTextView.setText("Testing");
RelativeLayout.LayoutParams layoutForAlignmentX = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
layout.addView(myTextView, layoutForAlignmentX);
很抱歉,如果您不理解某些内容,我的英语不是很好……我认为您应该为TextView创建一个新的RelativeLayoutParams,因为您将获得一个参数,该规则与其父项右侧对齐(按钮的参数) 您还必须为按钮提供一个id 你可以:
Button button = new Button(this);
upgradeButton.setId(12345);
您应该为按钮提供id:
Button button = new Button(this);
button.setId(12345);
证明此代码:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
Button button = new Button(this);
button.setId(12345);
RelativeLayout.LayoutParams layoutForAlignment = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignment.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(button, layoutForAlignment);
TextView myTextView = new TextView(getApplicationContext());
myTextView.setText("Testing");
RelativeLayout.LayoutParams layoutForAlignmentX = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
layout.addView(myTextView, layoutForAlignmentX);
对不起,如果你不懂什么,我的英语不是很好……不,在这里发帖时是打字错误。对不起,不,这是在这里发布时的拼写错误。对不起,谢谢!将ID设置为upgradeButton是一个输入错误,我在我的主要帖子中修复了它,但问题是我应该像你说的那样创建一个新的RelativeLayoutParams。谢谢!将ID设置为upgradeButton是一个错误,我在我的主要帖子中修复了它,但问题是我应该像你说的那样创建一个新的RelativeLayoutParams。