当单击按钮时,一个for循环开始,在for循环中,我正在增加文本大小并记录TextView的宽度。但是它并没有增加大小并且总是具有相同的宽度(请查看下面添加的日志)。我添加了完整的代码。该如何解决?
activity_main.xml
<RelativeLayout 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"
tools:context=".Mainactivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:fontFamily="@font/roboto"
android:text="00:00" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="ocBtnIncrease"
android:text="Increase" />
</RelativeLayout>
Mainactivity.java
package com.example.test;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatactivity;
public class Mainactivity extends AppCompatactivity {
public static final String TAG = Mainactivity.class.getSimpleName();
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
}
public void ocBtnIncrease(View view) {
for (int i = 1; i < 500; i++) {
tv.setTextSize(i);
Log.i(TAG,"i: " + i);
Log.i(TAG,"WIDTH: " + tv.getWidth());
SystemClock.sleep(500);
}
}
}
登录
I/Mainactivity: i: 1
I/Mainactivity: WIDTH: 71
I/Mainactivity: i: 2
I/Mainactivity: WIDTH: 71
I/Mainactivity: i: 3
I/Mainactivity: WIDTH: 71
I/Mainactivity: i: 4
.
.