简体   繁体   中英

Android - Resize Text to fit inside a TextView

I'm trying to resize the wrapping text in a TextView to fit within its bounds. I've created a new class with the source from here and I've added it inside my layout:

            <com.util.AutoResizeTextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_alignLeft="@id/ivBulb"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="80dp"
                android:layout_below="@id/ivBulb"
                android:layout_marginRight="10dp"
                android:id="@+id/textP"
                android:ellipsize="none"
                android:fontFamily="sans-serif"
                android:textColor="#000"
                android:textSize="100sp"/> <!--maximum size -->

The text that should be displayed inside the TextView is something like:

The sunset filled the entire sky with the deep color of rubies, setting the clouds ablaze. The waves crashed and danced along the shore, moving up and down in a graceful and gentle rhythm like they were dancing.

Some texts can be a multiline but some don't. My problem is that the text font size is small (about 35dp) and it's always the same :( I've added a ScrollView in order to be able to display all the text but I want to keep just the TextView and the text to resize

I'm new in Android development so please be gentle.

use from android.support.v7.widget.AppCompatTextView and set

android:ellipsize="none"
                                android:singleLine="true"
                                app:autoSizeMaxTextSize="14sp"
                                app:autoSizeMinTextSize="12sp"
                                app:autoSizeStepGranularity="0.5sp"
                                app:autoSizeTextType="uniform"

in

<android.support.v7.widget.AppCompatTextView
                                android:ellipsize="none"
                                android:singleLine="true"
                                app:autoSizeMaxTextSize="14sp"
                                app:autoSizeMinTextSize="12sp"
                                app:autoSizeStepGranularity="0.5sp"
                                app:autoSizeTextType="uniform"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"              android:id="@+id/txt_ortopedi" 
                                android:textColor="@color/text_color"
                                android:padding="4dp" android:hint="@string/search"
                                android:gravity="clip_vertical|center_horizontal" android:layout_gravity="center"
                                android:background="@color/white"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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