Johnny Wu
Johnny Wu

Reputation: 1518

PagerTabStrip is clipping off the icon

The design called for just icon on the tab of the PagerTabStrip.

Here's the XML:

<android.support.v4.view.PagerTabStrip
        android:id="@+id/tbstrp_album"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        android:padding="0dp"
        android:background="@color/offWhite">

Here's my getPageTitle inside the adapter:

@Override
public CharSequence getPageTitle(int position) {
    // This is "generic" string that we will use as the title to be replaced.
    String title = "title";

    Drawable myDrawable = oContext.getDrawable(R.drawable.alpha);

    // initiate the SpannableString builder
    SpannableStringBuilder spanBuilder = new SpannableStringBuilder(title); // space added before text for convenience

    // set the drawable's size...if it could be too big or too small for display
    myDrawable.setBounds(0, 0, 50, 50);

    // turn the Drawable into ImageSpan and align it along the baseline
    ImageSpan imageSpan = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);

    // CRUCIAL: this is where we replace the "title" w/ the image
    // 0: we start from the beginning
    // title.length(): we are replacing the entire string
    // the last flag doesn't do anything in our case
    spanBuilder.setSpan(imageSpan, 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spanBuilder;
}

The output looks like this:

enter image description here

What's causing the strip clipping the image? Thanks!

Upvotes: 1

Views: 76

Answers (1)

Johnny Wu
Johnny Wu

Reputation: 1518

It turns out this statement is the culprit:

myDrawable.setBounds(0, 0, 50, 50);

I changed the second argument which is the "top" to 30 and the icon was not clipped.

The unanswered question is still...why do I have to specify a value for the "top" argument.

Upvotes: 0

Related Questions