如何以编程方式添加 TextView 并在布局中对齐它们
How to programmatically add TextViews and align them in a Layout
我想创建一些 TextView(和按钮),然后将它们放在 ConstraintLayout 中的 RelativeLayout 上(也可以是 LinearLayout 或任何其他可行的东西)...
我的目标是为每个按钮在其左侧放置一个文本视图,并将它们居中放置在布局中。 (这就是我想用 Layoutparams 做的)。
但为什么我会收到 NullPointer 异常?
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/welcomeiqlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.william.httprequest.Welcome_InitialQuestions">
<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:text="@string/wilkommen"
android:textAlignment="center"
android:textSize="35sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView28"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="24dp"
android:text="@string/wilkommen_t1"
android:textAlignment="center"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView14" />
<Button
android:id="@+id/startinitialbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="@color/aqua"
android:elevation="0dp"
android:text="@string/start_btn"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<RelativeLayout
android:id="@+id/onetimerelative"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/startinitialbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView28"
app:layout_constraintVertical_bias="1.0">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Java代码:
@Override
public void onResponse(String response) {
//Log.i(TAG, "MY_PROFILE_DATA: "+ response);
studiesList = new LinkedList<String>();
try {
JSONObject jsonObject1 = new JSONObject(response);
JSONArray jArray = jsonObject1.getJSONArray("data");
if (jArray != null) {
for (int i = 0; i < jArray.length(); i++) {
studiesList.add(jArray.getString(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Iterator<String> itr=studiesList.iterator();
//Log.i(TAG,"Questionnaire_DATA: "+ studiesList.toString());
int i = 1;
while(itr.hasNext())
{
if(i==1) {
Log.i(TAG, "Questionnaire_DATA: " + itr.next());
//add textViews
TextView textView = new
TextView(getApplicationContext());
textView.setText("Fragebogen " + i);
//add buttons
Button button = new Button(getApplicationContext());
button.setText("Offen");
button.setId(i);
RelativeLayout.LayoutParams lp =
(RelativeLayout.LayoutParams)
otrelativeLayout.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.RIGHT_OF, button.getId());
lp.addRule(RelativeLayout.ALIGN_BASELINE, button.getId());
textView.setLayoutParams(lp);
otrelativeLayout.addView(button);
otrelativeLayout.addView(textView);
i++;
} else {
}
}
错误:
FATAL EXCEPTION: main
Process: com.example.william.httprequest, PID: 26073
java.lang.ClassCastException:
android.support.constraint.ConstraintLayout$LayoutParams cannot be cast to
android.widget.RelativeLayout$LayoutParams
如果有任何帮助,我将不胜感激:)
View.getLayoutParams() 的 Android 文档声明返回的布局参数提供给此视图的 parent。
这解释了 ClassCastException
:当在你的 RelativeLayout
上调用 getLayoutParams()
时,你正在检索描述它应该如何在其父 ConstraintLayout
中布局的布局参数,因此是ConstraintLayout.LayoutParams
.
您必须为每个要添加为 [=12= 的子项的 TextView
或 Button
实例化一个新的 RelativeLayout.LayoutParams
,配置其参数,用 View.setLayoutParams(ViewGroupe.LayoutParams)
设置它,然后将新创建的视图添加到它的 RelativeLayout
父视图。
我想创建一些 TextView(和按钮),然后将它们放在 ConstraintLayout 中的 RelativeLayout 上(也可以是 LinearLayout 或任何其他可行的东西)... 我的目标是为每个按钮在其左侧放置一个文本视图,并将它们居中放置在布局中。 (这就是我想用 Layoutparams 做的)。 但为什么我会收到 NullPointer 异常?
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/welcomeiqlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.william.httprequest.Welcome_InitialQuestions">
<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:text="@string/wilkommen"
android:textAlignment="center"
android:textSize="35sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView28"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="24dp"
android:text="@string/wilkommen_t1"
android:textAlignment="center"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView14" />
<Button
android:id="@+id/startinitialbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="@color/aqua"
android:elevation="0dp"
android:text="@string/start_btn"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<RelativeLayout
android:id="@+id/onetimerelative"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/startinitialbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView28"
app:layout_constraintVertical_bias="1.0">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Java代码:
@Override
public void onResponse(String response) {
//Log.i(TAG, "MY_PROFILE_DATA: "+ response);
studiesList = new LinkedList<String>();
try {
JSONObject jsonObject1 = new JSONObject(response);
JSONArray jArray = jsonObject1.getJSONArray("data");
if (jArray != null) {
for (int i = 0; i < jArray.length(); i++) {
studiesList.add(jArray.getString(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Iterator<String> itr=studiesList.iterator();
//Log.i(TAG,"Questionnaire_DATA: "+ studiesList.toString());
int i = 1;
while(itr.hasNext())
{
if(i==1) {
Log.i(TAG, "Questionnaire_DATA: " + itr.next());
//add textViews
TextView textView = new
TextView(getApplicationContext());
textView.setText("Fragebogen " + i);
//add buttons
Button button = new Button(getApplicationContext());
button.setText("Offen");
button.setId(i);
RelativeLayout.LayoutParams lp =
(RelativeLayout.LayoutParams)
otrelativeLayout.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.RIGHT_OF, button.getId());
lp.addRule(RelativeLayout.ALIGN_BASELINE, button.getId());
textView.setLayoutParams(lp);
otrelativeLayout.addView(button);
otrelativeLayout.addView(textView);
i++;
} else {
}
}
错误:
FATAL EXCEPTION: main
Process: com.example.william.httprequest, PID: 26073
java.lang.ClassCastException:
android.support.constraint.ConstraintLayout$LayoutParams cannot be cast to
android.widget.RelativeLayout$LayoutParams
如果有任何帮助,我将不胜感激:)
View.getLayoutParams() 的 Android 文档声明返回的布局参数提供给此视图的 parent。
这解释了 ClassCastException
:当在你的 RelativeLayout
上调用 getLayoutParams()
时,你正在检索描述它应该如何在其父 ConstraintLayout
中布局的布局参数,因此是ConstraintLayout.LayoutParams
.
您必须为每个要添加为 [=12= 的子项的 TextView
或 Button
实例化一个新的 RelativeLayout.LayoutParams
,配置其参数,用 View.setLayoutParams(ViewGroupe.LayoutParams)
设置它,然后将新创建的视图添加到它的 RelativeLayout
父视图。