Android - 使文本视图自动调整大小的文本以适合布局
Android - Making textview auto resizable text to fit layout
我正在尝试获取文本视图中文本的大小以便稍后在代码中使用它。
我已经声明了一个 float 和我的 textview
private float mStartTextSize;
private TextView mWordTextView;
然后在我的 onCreate 中我有
mWordTextView = (TextView) findViewById(R.id.factTextView);
mStartTextSize = mWordTextView.getTextSize();
但是当我调试和使用我的 phone 时,即使我在 xml 中将它设置为 30sp,它也会得到 105 的值。
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/choose_teams"
android:singleLine="false"
android:id="@+id/wordTextView"
android:textSize="30sp"
android:textColor="@android:color/white"
android:textStyle="bold|italic"
android:layout_below="@+id/titleTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:gravity="top|center"/>
您收到的尺寸是像素而不是 sp。
getTextSize
float getTextSize ()
Returns
float the size (in pixels) of the default text size in this TextView.
您可以使用它来隐藏 sp/dp 到像素并返回而不需要上下文。
public static float pxToDp(int px) {
return px / Resources.getSystem().getDisplayMetrics().density;
}
public static float dpToPx(int dp) {
return dp * Resources.getSystem().getDisplayMetrics().density;
}
我正在尝试获取文本视图中文本的大小以便稍后在代码中使用它。
我已经声明了一个 float 和我的 textview
private float mStartTextSize;
private TextView mWordTextView;
然后在我的 onCreate 中我有
mWordTextView = (TextView) findViewById(R.id.factTextView);
mStartTextSize = mWordTextView.getTextSize();
但是当我调试和使用我的 phone 时,即使我在 xml 中将它设置为 30sp,它也会得到 105 的值。
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/choose_teams"
android:singleLine="false"
android:id="@+id/wordTextView"
android:textSize="30sp"
android:textColor="@android:color/white"
android:textStyle="bold|italic"
android:layout_below="@+id/titleTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:gravity="top|center"/>
您收到的尺寸是像素而不是 sp。
getTextSize
float getTextSize ()
Returns
float the size (in pixels) of the default text size in this TextView.
您可以使用它来隐藏 sp/dp 到像素并返回而不需要上下文。
public static float pxToDp(int px) {
return px / Resources.getSystem().getDisplayMetrics().density;
}
public static float dpToPx(int dp) {
return dp * Resources.getSystem().getDisplayMetrics().density;
}