Я пытаюсь создать макет программно, но он не работает. Вот версия XML, которая выглядит правильно.Android - RelativeLayout программно
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/checkBox1"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
который выглядит следующим образом:
Это версия Java:
RelativeLayout.LayoutParams lp;
RelativeLayout rl = new RelativeLayout(this);
lp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rl.setLayoutParams(lp);
CheckBox cbox = new CheckBox(this);
lp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
cbox.setLayoutParams(lp);
EditText etext = new EditText(this);
etext.setText("TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView");
lp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_BASELINE, cbox.getId());
lp.addRule(RelativeLayout.ALIGN_BOTTOM, cbox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.RIGHT_OF, cbox.getId());
etext.setLayoutParams(lp);
rl.addView(cbox);
rl.addView(etext);
this.setContentView(rl);
Это выход:
Что я делаю неправильно? Благодарю.
Возможно, вы не по теме, но считаете ли вы использование [CheckedTextView] (http://developer.android.com/reference/android/widget/CheckedTextView.html)? – stkent
Я не знал, что он существует. Благодаря! –