Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why custom Preference has larger text size?

I have a custom preference for showing icon with title and summary. But I don't know why its title has a larger text size compared to the normal preferences (built in one). Here are the source and layout xml of my custom preference. If you need more info, please let me know. Thank You.

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;
    }    
}

Layout file here

<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>
like image 431
Bear Avatar asked Feb 03 '26 11:02

Bear


1 Answers

Your problem is likely that your title uses android:attr/textAppearanceLarge. Digging through the Android source code, the preferences_child.xml resource instead uses ?android:attr/textAppearanceMedium.

like image 122
Alex Curran Avatar answered Feb 05 '26 00:02

Alex Curran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!