一、xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="test"
android:textColor="@color/colorAccent"
android:textSize="14sp" />
android:id="@+id/btn_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_btn"
android:text="text-length-change"
android:textColor="@color/colorPrimary"
android:textSize="13sp" />
</LinearLayout>
二、selector_btn
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<size android:width="20dp" android:height="20dp" />
<corners android:radius="5dp" />
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
<stroke android:width="1dp" android:color="@android:color/holo_blue_bright" android:dashWidth="10dp" android:dashGap="2dp" />
<gradient android:angle="0" android:endColor="@android:color/holo_orange_dark" android:startColor="@android:color/holo_orange_light" />
<item android:state_pressed="true">
<shape android:shape="rectangle">
<size android:width="20dp" android:height="20dp" />
<corners android:radius="5dp" />
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
<stroke android:width="1dp" android:color="@android:color/holo_blue_bright" android:dashWidth="10dp" android:dashGap="2dp" />
<gradient android:angle="0" android:endColor="@android:color/holo_orange_light" android:startColor="@android:color/holo_orange_dark" />
</selector>
三、代码
package lina.simple.com.testdemo;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
private TextViewtvTest;
private ButtonbtnPress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initBase();
}
private void initBase() {
}
private void textViewSet() {
tvTest.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
Paint mPaint =tvTest.getPaint();
String text =tvTest.getText().toString();
int width =tvTest.getMeasuredWidth();
if (width >0) {
int realWidth = width -tvTest.getPaddingLeft() -tvTest.getPaddingRight();
float trySize =tvTest.getTextSize();
while (mPaint.measureText(text) > realWidth) {
trySize -=2;
tvTest.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
}
}
}
private void initView() {
tvTest = findViewById(R.id.tv_test);
btnPress = findViewById(R.id.btn_press);
btnPress.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_press:
showText();
break;
}
}
private void showText() {
Random mRandom =new Random();
switch (mRandom.nextInt(3)) {
case 0:
tvTest.setText("文字长度1:textView自动调节文字大小");
break;
case 1:
tvTest.setText("文字长度1:textView自动调节文字大小;文字长度1:textView自动调节文字大小;");
break;
case 2:
tvTest.setText("文字长度1:textView自动调节文字大小;文字长度1:textView自动调节文字大小;文字长度1:textView自动调节文字大小;");
break;
}
textViewSet();
}
}
网友评论