更改首选项的高度

时间:2013-12-15 15:06:03

标签: android android-preferences

我有一个PreferenceFragment子类。我希望它的每个项目(Preferences和SwitchPreferences)的高度为120dp。怎么做?

以下是相关代码:

public class SettingsFragment extends PreferenceFragment {
        public SettingsFragment() {}
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.main);         
        }
}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference android:key="app_main_switch"
                      android:title="@string/app_name"
                      android:defaultValue="true"/>
    <Preference android:title="@string/events_lowercase"
                android:dependency="app_main_switch">
        <intent android:targetPackage="hu.ppke.itk.marma.android.bead" 
                android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
    </Preference>
    <Preference android:title="@string/filters_lowercase"
                android:dependency="app_main_switch">
        <intent android:targetPackage="hu.ppke.itk.marma.android.bead" 
                android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
    </Preference>
    <SwitchPreference android:dependency="app_main_switch"
                      android:key="learn_switch"
                      android:defaultValue="false"
                      android:title="@string/learning"/>
</PreferenceScreen>

以下是现在的样子:

enter image description here

所以我希望列表中的所有四个项目的高度都是120dp。正如您所看到的,我不是创建ListView的人,而是在内部创建的。我试图用

检索它
findViewById(android.R.id.list)

但迭代其元素会产生不允许我设置高度的Preference对象。

3 个答案:

答案 0 :(得分:4)

这样做:

<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Min item height -->
    <item name="android:listPreferredItemHeight">10dp</item>
</style>

可以在此处找到可以覆盖的其他样式属性preference item layout

答案 1 :(得分:1)

尝试创建自定义Preference类(您可能必须为要使用的每种类型的Preference执行此操作/希望高度为120DP)

public class SwitchPref extends SwitchPreference {

Context context;

public Pref(Context context) {
    super(context);

    this.context = context;
}

@Override
protected View onCreateView(ViewGroup parent) {

    LinearLayout layout = new LinearLayout(getContext());

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(120));

    layout.setLayoutParams(params1);

       //if this returns just the linearlayout, you will have to add your own switch
       // or reference to a layout.xml resource

    return super.onCreateView(layout);

}

public int dpToPx(int dp) {
    int valueInDp = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
                    .getDisplayMetrics());
    return valueInDp;
}

}

然后,在您的preference.xml中

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <!--use your custom SwitchPrefence class-->
    <my.package.name.SwitchPref android:key="app_main_switch"
                  android:title="@string/app_name"
                  android:defaultValue="true"/>
    <Preference android:title="@string/events_lowercase"
            android:dependency="app_main_switch">
        <intent android:targetPackage="hu.ppke.itk.marma.android.bead" 
            android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
    </Preference>
    <Preference android:title="@string/filters_lowercase"
            android:dependency="app_main_switch">
        <intent android:targetPackage="hu.ppke.itk.marma.android.bead" 
            android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
    </Preference>
    <my.package.name.SwitchPref android:dependency="app_main_switch"
                  android:key="learn_switch"
                  android:defaultValue="false"
                  android:title="@string/learning"/>
</PreferenceScreen>

希望这有帮助,快乐编码!

答案 2 :(得分:1)

以下是我设法做到这一点的方法:

我确实需要继承Preference,但MattMatt的答案不起作用。

public class HigherPreference extends Preference {
    public HigherPreference(Context context) {
        super(context);
    }
    public HigherPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }   
    @Override
    protected View onCreateView(ViewGroup parent) {
        Resources res=getContext().getResources();
        View v=super.onCreateView(parent);
        v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.rowheight)));
        return v;
    }
}

布局XML与他提供的一样。