相对布局以编程方式添加视图一个低于另一个未正确对齐
Relative Layout adding views programmatically one below other not aligning correctly
我正在尝试以编程方式将文本视图添加到相对布局父级中的一个下方。但是,第一个视图未正确对齐,而其余视图已正确对齐。见附图。
我的代码:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
相对布局 xml 我将视图添加到的位置:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="@drawable/rectangle_border"
android:text="Back"
android:textColor="@android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="@drawable/rectangle_border"
android:text="Next"
android:textColor="@android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
我阅读了一些与此问题相关的帖子并遵循了他们的指导方针,但这没有帮助。
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
我使用 for
循环的索引作为 textView 的 id,我将其更改为 Android 为它生成如下所示的 Id,它现在按预期正常工作。
我改线了
textView.setId(i)
至
textView.setId(ViewGroup.generateViewId())
谢谢。
我正在尝试以编程方式将文本视图添加到相对布局父级中的一个下方。但是,第一个视图未正确对齐,而其余视图已正确对齐。见附图。
我的代码:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
相对布局 xml 我将视图添加到的位置:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="@drawable/rectangle_border"
android:text="Back"
android:textColor="@android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="@drawable/rectangle_border"
android:text="Next"
android:textColor="@android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
我阅读了一些与此问题相关的帖子并遵循了他们的指导方针,但这没有帮助。
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
我使用 for
循环的索引作为 textView 的 id,我将其更改为 Android 为它生成如下所示的 Id,它现在按预期正常工作。
我改线了
textView.setId(i)
至
textView.setId(ViewGroup.generateViewId())
谢谢。