2017-08-30 71 views
0

因此,我正在使用库调整文本大小以适合范围:https://github.com/grantland/android-autofittextview。我想要做的是循环访问文本视图的ArrayList并查找最小文本大小,然后将该文本大小设置为每个文本视图。 我的Java代码:Android - 设置文本大小以编程方式切断单词

//Loop through every textview and get lowest size 
     float lowestTextSize = textViews.get(0).getTextSize(); 
     for (TextView textView : textViews){ 
      if(lowestTextSize > textView.getTextSize()){ 
       lowestTextSize = textView.getTextSize(); 
      } 
     } 

     lowestTextSize = lowestTextSize/getResources().getDisplayMetrics().scaledDensity; 

     //Loop through every textview and set them all to the lowest text size previously found 
     for (TextView textView : textViews){ 
      textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize); 
     } 

我的问题是,大小设置为每隔后的TextView的TextView的变化大小,但没有的话本身的大小,所以我得到削减了一半的话(例外制作字whos大小我正在使用,调整大小的AutoFit库): enter image description here

有关如何解决此问题的任何想法?

我的XML代码:

<LinearLayout 
    android:id="@+id/highestSellingProductLayout" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="8dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <me.grantland.widget.AutofitTextView 
      android:id="@+id/highestSellingProduct" 
      autofit:minTextSize="15sp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:maxLines="1" 
      android:paddingBottom="5dp" 
      android:text="NA" 
      android:textAlignment="center" 
      android:textColor="@color/colorSuccess" 
      android:textSize="22sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <me.grantland.widget.AutofitTextView 
      autofit:minTextSize="15sp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="bottom" 
      android:maxLines="1" 
      android:paddingBottom="5dp" 
      android:text="Highest Selling" 
      android:textAlignment="center" 
      android:textSize="15sp" /> 
    </LinearLayout> 

</LinearLayout> 
+2

增加布局高度 –

+0

它是没有用的。因为我的目的是根据文本的最小尺寸调整文本的大小......因为您可以看到文本未被调整大小,所以文本的大小保持不变...... – Alphonse

+0

getTextSize()返回文本的实际像素尺寸。尝试TypedValue.COMPLEX_UNIT_PX – PrisonMike

回答

0

我已经找到了问题。要使用AutofitTextView更改java中的文本大小,我必须在每个textview上使用autofitHelper。以下代码适用于我:

//Loop through every textview and get lowest size 
    float lowestTextSize = textViews.get(0).getTextSize(); 
    for (TextView textView : textViews){ 
     if(lowestTextSize > textView.getTextSize()){ 
      lowestTextSize = textView.getTextSize(); 
     } 
    } 

    lowestTextSize = lowestTextSize/getResources().getDisplayMetrics().scaledDensity; 

    //Loop through every textview and set them all to the lowest text size previously found 
    for (TextView textView : textViews){ 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize); 
     AutofitHelper autofitHelper = AutofitHelper.create(textView); 
     autofitHelper.setTextSize(textView.getTextSize()); 
    } 
相关问题