Android需要设计一个CheckBox
Need to design a CheckBox in Android
我在 Android 中动态创建复选框,它们变得像这样:
我想要:
我该怎么做?
利用重力属性实现你所需要的
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem"
android:gravity="top"/>
您还可以通过将 TextView 和 CheckBox 放入布局中来动态添加
然后将布局添加到您的父布局。
假设您有一个名为 parentLayout 的父布局,您可以:
CheckBox cb = new CheckBox(context);
TextView tv = new TextView(context);
tv.setText("what ever you want...");
tv.setGravity(Gravity.LEFT | Gravity.TOP); //Aligning the text itself inside the textview to the left (for LTR language)
// LinearLayout Container
LinearLayout ll_container = new LinearLayout(context);
ll_container.setOrientation(LinearLayout.HORIZONTAL);
ll_container.setGravity(Gravity.LEFT | Gravity.TOP); //Aligning the textview and the checkbox to the left inside the ll_container
ll_container.addView(cb);
ll_container.addView(tv);
parentLayout.addView(ll_container);
parentLayout.refreshDrawableState();
我在 Android 中动态创建复选框,它们变得像这样:
我想要:
我该怎么做?
利用重力属性实现你所需要的
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem"
android:gravity="top"/>
您还可以通过将 TextView 和 CheckBox 放入布局中来动态添加
然后将布局添加到您的父布局。
假设您有一个名为 parentLayout 的父布局,您可以:
CheckBox cb = new CheckBox(context);
TextView tv = new TextView(context);
tv.setText("what ever you want...");
tv.setGravity(Gravity.LEFT | Gravity.TOP); //Aligning the text itself inside the textview to the left (for LTR language)
// LinearLayout Container
LinearLayout ll_container = new LinearLayout(context);
ll_container.setOrientation(LinearLayout.HORIZONTAL);
ll_container.setGravity(Gravity.LEFT | Gravity.TOP); //Aligning the textview and the checkbox to the left inside the ll_container
ll_container.addView(cb);
ll_container.addView(tv);
parentLayout.addView(ll_container);
parentLayout.refreshDrawableState();