2013-12-15 64 views
3

我有一个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) 

但它迭代其元素给出不允许我设置高度的偏好设置对象。

+0

你能告诉我们一些代码吗?这将有助于确定并解决您的问题。 – MattMatt

+0

谢谢,尝试创建一个扩展SwitchPreference的自定义Preference类并在preference.xml中使用它。看到我的回答 – MattMatt

回答

1

这是我如何成功地做到这一点:

我也需要继承的偏好,但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就像他所提供的。

+0

AbsListView从哪里来? – Jacky

+0

@Jacky [android.widget.abslistview](http://developer.android.com/reference/android/widget/AbsListView.html) – marczellm

+0

非常感谢,工作就像一个魅力!提示其他人:'rowheight'进入'res/values/dimens.xml'。 –

1

尝试创建一个自定义的偏好类(您可能必须这样做对所有类型的偏好,你要使用/要高度是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> 

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

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

+0

感谢您的回答。我现在无法尝试,因为我最近没有做任何Android开发。 – marczellm

+1

没关系,也许有人觉得它很有用。 –