RoundedCornersBackgroundSpan icon indicating copy to clipboard operation
RoundedCornersBackgroundSpan copied to clipboard

End of single line background span cut off when using ALIGN_CENTER

Open luca992 opened this issue 7 years ago • 0 comments

When setting a textview's span with the parameter ALIGN_CENTER, the end of the background is cut off. This only happens when the text does not wrap to the next line.

Also adding a space to the end of text causes the background to display correctly... but the background does not fit the text correctly anymore.

When text wraps to multiple lines it is displayed correctly

Here is an example activity and layout:

example

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.widget.TextView
import com.github.iojjj.rcbs.RoundedCornersBackgroundSpan

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val testTv = findViewById<TextView>(R.id.testTv)
        val test2Tv = findViewById<TextView>(R.id.test2Tv)
        val test3Tv = findViewById<TextView>(R.id.test3Tv)
        testTv.gravity = Gravity.CENTER_HORIZONTAL
        test2Tv.gravity = Gravity.CENTER_HORIZONTAL
        test3Tv.gravity = Gravity.CENTER_HORIZONTAL
        val padding = convertDpToPixel(4f, this)
        val radius = convertDpToPixel(5f, this)
        val span = RoundedCornersBackgroundSpan.Builder(this)
                .addTextPart("Test",0x990000CC.toInt())
                .setCornersRadius(radius)
                .setTextPadding(padding)
                .setTextAlignment(RoundedCornersBackgroundSpan.ALIGN_CENTER)
                .build()
        testTv.setText(span)
        val span2 = RoundedCornersBackgroundSpan.Builder(this)
                .addTextPart("Test ",0x990000CC.toInt())
                .setCornersRadius(radius)
                .setTextPadding(padding)
                .setTextAlignment(RoundedCornersBackgroundSpan.ALIGN_CENTER)
                .build()
        test2Tv.setText(span2)
        val span3 = RoundedCornersBackgroundSpan.Builder(this)
                .addTextPart("Test Test Test",0x990000CC.toInt())
                .setCornersRadius(radius)
                .setTextPadding(padding)
                .setTextAlignment(RoundedCornersBackgroundSpan.ALIGN_CENTER)
                .build()
        test3Tv.setText(span3)
    }


    fun convertDpToPixel(dp: Float, context: Context): Float {
        val resources = context.resources
        val metrics = resources.displayMetrics
        return dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
    }

}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lucaspinazzola.testbug.MainActivity">

    <TextView
        android:id="@+id/testTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="10dp"
        android:padding="4dp"
        android:shadowColor="#00000001"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="16"
        android:textSize="80sp"
        android:textStyle="bold"
        android:typeface="sans"
        app:layout_constraintBottom_toTopOf="@+id/test2Tv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/test2Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="10dp"
        android:padding="4dp"
        android:shadowColor="#00000001"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="16"
        android:textSize="80sp"
        android:textStyle="bold"
        android:typeface="sans"
        app:layout_constraintBottom_toTopOf="@+id/test3Tv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/testTv" />

    <TextView
        android:id="@+id/test3Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="10dp"
        android:padding="4dp"
        android:shadowColor="#00000001"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="16"
        android:textSize="80sp"
        android:textStyle="bold"
        android:typeface="sans"
        app:layout_constraintTop_toBottomOf="@+id/test2Tv"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

luca992 avatar Nov 30 '17 11:11 luca992