简体   繁体   English

PreferenceScreen android:paddingTop无法正常工作

[英]PreferenceScreen android:paddingTop not working

With normal layout file, I can set padding top like this 使用普通的布局文件,我可以像这样设置顶部填充

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"

>

However, with preference screen, it renders the padding short of some pixels (the action bar is 168 dp, but the padding appears only 144), any idea how to fix this cross-device way. 但是,使用首选项屏幕时,它将使填充不足一些像素(操作栏为168 dp,但是填充仅显示144 d),这是解决这种跨设备方式的任何想法。 Thanks. 谢谢。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingTop="?attr/actionBarSize">

When using PreferenceScreen you don't have access to those xml attributes. 使用PreferenceScreen您无权访问这些xml属性。 But what you can do is add this attribute to it: 但是您可以做的是向此属性添加以下属性:

android:layout="@layout/header_setting"

Then create a XML layout file called in this example "header_settings" you can change the sizes and padding as normal. 然后创建一个在此示例中称为“ header_settings”的XML布局文件,您可以照常更改大小和填充。 But applying the id's to the elements the PreferenceScreen knows where the titles goes or icon. 但是将id应用于元素,PreferenceScreen便知道标题或图标的位置。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:padding="0dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+android:id/icon"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_width="40dp"
        android:layout_height="40dp" />

    <TextView android:id="@+android:id/title"
        android:layout_marginLeft="80dp"
        android:textColor="#000"
        android:layout_marginTop="23dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>
</LinearLayout>

And now here is an example on how the PreferenceScreen should look like: 现在,这是一个有关PreferenceScreen外观的示例:

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


    <PreferenceCategory>
        <PreferenceScreen
            android:icon="@drawable/setting_star"
            android:layout="@layout/header_setting"
            android:title="Upgrade to Pro!">

        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

Notice how you only have access to the attributes: title, summary, and icon. 请注意,您只能访问以下属性:标题,摘要和图标。 The layout attribute lets you customize it to whatever you want to do. layout属性使您可以自定义它以进行任何操作。 In the XML you must keep the id's the same since thats how the PreferenceScreen knows thats were to put the title text or the icon image. 在XML中,您必须保持id不变,因为那是PreferenceScreen知道如何放置标题文本或图标图像的方式。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM