Android PreferenceActivity Item Height
-
28-10-2019 - |
문제
I have a PreferenceActivity with two checkboxes. Everything is working correctly, but I have a UI problem; not all of the text for the checkbox fits onto the checkbox row.
Is there a nice way to set the minimum height for these checkbox preferences without hardcoding a value?
EDIT - adding xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="alarm_set"
android:title="@string/set_alarm_title"
android:defaultValue="true" />
<CheckBoxPreference
android:key="store_pages"
android:title="@string/store_downloaded_pages_title"
android:summary="@string/store_downloaded_pages"
android:defaultValue="false" />
</PreferenceScreen>
해결책
Based off this question about text preference having the same iussue I use this class.
public class LongSummaryCheckBoxPreference extends CheckBoxPreference
{
public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs, int defStyle)
{
super(ctx, attrs, defStyle);
}
public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs)
{
super(ctx, attrs);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView summary= (TextView)view.findViewById(android.R.id.summary);
summary.setMaxLines(4);
}
}
In the xml it then looks like this.
<com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_distance_line_enable"
android:title="@string/title_distance_line_enable" android:summary="@string/summary_distance_line_enable"
android:defaultValue="true" />
<com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_altitude_line_enable"
android:title="@string/title_altitude_line_enable" android:summary="@string/summary_altitude_line_enable"
android:defaultValue="true" />
I have exactly the same thing for EditTextPreference which has the same problem of a max of 2 lines for the summary in Api <= 7
다른 팁
In the xml you can set the height to "wrap_content". At that point it will size it to fit whatever you put in it. So, something along these lines:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
This should help
Just do it this way:
<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Min item height -->
<item name="android:listPreferredItemHeight">10dp</item>
</style>
another styling attributes that can be overridden can be found here preference item layout