更改偏好设置项的高度。

4

我有一个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对象,这些对象不允许我设置高度。

你能展示一些代码吗?这将有助于识别和解决你的问题。 - MattMatt
谢谢,尝试创建一个自定义的Preference类,扩展SwitchPreference,并在preference.xml中使用它。请参阅我的答案。 - MattMatt
3个回答

6

只需按照以下方式操作:

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

还有其他可以被覆盖的样式属性,可以在偏好项布局中找到。


谢谢你的回答。我现在无法尝试它,因为我最近没有进行任何Android开发。 - marczellm
1
没关系,也许有人会觉得它有用。 - Nikolay Nikiforchuk

2
尝试创建自定义偏好设置类(您可能需要为每种要使用/希望高度为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

以下是我是如何完成这个操作的:

我确实需要子类化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的格式与他提供的相同。

AbsListView来自哪里? - LiangWang
@Jacky android.widget.abslistview - marczellm
太好了,谢谢,运行得很顺利!给其他人的提示:rowheight 放在 res/values/dimens.xml 中。 - peter_the_oak
你注意到这种情况下切换动画丢失了吗? - Vikram Gupta
终于,在漫长的搜索之后,找到了一个可行的答案。 - Fred

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接