为什么自定义偏好设置有更大的文本大小?

3

我有一个自定义的偏好设置,希望在标题和摘要中显示图标。但是我不知道为什么它的标题相比于普通偏好设置(内置的)具有更大的文本大小。以下是我的自定义偏好设置的源代码和布局XML。如果您需要更多信息,请告诉我。谢谢。

public class IconPreferenceScreen extends Preference {

    public Drawable mIcon;
    private ImageView mImageView;

    public IconPreferenceScreen(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.preferenceStyle);
    }

    public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayoutResource(R.layout.preference_icon);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.IconPreferenceScreen, defStyle, android.R.attr.preferenceStyle);
        mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_iconPref);
        a.recycle();
    }

    public IconPreferenceScreen(Context context) {
        this(context, null);
        setLayoutResource(R.layout.preference_icon);
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        mImageView = (ImageView) view.findViewById(R.id.icon);
        if (mImageView != null && mIcon != null) {
            mImageView.setImageDrawable(mIcon);
        }
    }

    public void setIcon(Drawable icon) {
        if (mImageView != null && icon != null) {
            mImageView.setImageDrawable(icon);
        }
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        View layout= super.onCreateView(parent);
        layout.setVisibility(View.VISIBLE);
        return layout;
    }    
}

这里是布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/widget_frame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dip"
        android:layout_marginRight="6dip"
        android:layout_gravity="center" />

</LinearLayout>
1个回答

2
你的问题可能是标题使用了 android:attr/textAppearanceLarge。通过查看Android源代码,preferences_child.xml 资源使用的是 ?android:attr/textAppearanceMedium。请注意保留HTML标签。

哦,我之前看的是 preferences.xml 而不是 preferences_child.xml。你觉得怎么样? - Bear
我只是使用那个网站。任何与Android核心相关的内容都可以在frameworks/base/core/中找到,任何应用程序中的内容都可以在packages/apps/中找到。值得一提的是,在该网站上四处看看,你会经常发现一些非常好的东西! - Alex Curran

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