Checkbox is over text, how to add padding?
I have a listview with checkboxes:
<LinearLayout xmlns:android=开发者_开发知识库"http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/nomeAPP" style="?listItem" />
</LinearLayout>
But the problem is: checkboxes is over text (text is to much for the left)
How can I correct that?
How about putting android:paddingLeft="xdp"
where is x is the number of dp that you will need to put in order to you text?
Putting padding will only shift text but not your "Tick-Button".
You may consider android:gravity
property too if putting a hard-coded dp doesn't suit your need.
If your text is in the same layout of the checkbox, do it like this:
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nomeAPP"
style="?listItem"
android:layout_marginLeft="10dp" />
But if its not, do it like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp">
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nomeAPP"
style="?listItem" />
</LinearLayout>
Obs: I've put margin and padding with left only for the example.
Let me know if you have more doubts. Thanks.
Here’s what I used for items in a list, each of which has text and a radio button. Note the layout_weight and layout_gravity settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/select_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_weight="100"
/>
<RadioButton
android:id="@+id/file_item_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_gravity="right"
/>
</LinearLayout>
There are two ways to resolve this issue_
1 Building your own custom CheckBox Class by extending android.widget.CheckBox
class. The Complete Custom code is as below_
public class CustomCheckBox extends CheckBox {
public CustomCheckBox(Context context) {
super(context);
}
public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public int getCompoundPaddingLeft() {
final float scale = this.getResources().getDisplayMetrics().density;
/*add padding you needed so the text is not on top of the
* CheckBox.
*/
return (super.getCompoundPaddingLeft() + (int) (10.0f * scale + 0.5f));
}
}
2 Set Padding in Your Activity_
final float scale =this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom());
I hope this will help all who have this issue... :)
精彩评论