遇到一个问题是这样的:当TextView显示的字符只有一行时,缩小文字的大小,View的高度不变。为什么会这样呢?我设置的TextView的高度为wrap_content的,为什么View的高度没有同样缩小?如果我想缩小它,该怎么办呢?
当TextView的文字显示为两行时候就不存在这个问题。这时候缩小文字的大小,View的高度也缩小。为了证实这个问题,我特意写里个Demo如下:1.字符串资源:<string name="one_line">AB</string>
<string name="two_line">AB\nCD</string>2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF00"/>
<Button
android:id="@+id/btn_changeSize"
android:text="ChangeSize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Button>
<Button
android:id="@+id/btn_changeTest"
android:text="ChangeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Button></LinearLayout>3.TestActivity.javapublic class TestActivity extends Activity {
TextView tvTest;
Button btnChangeSize;
Button btnChangeText;
boolean sizeChanged;
boolean textChanged;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvTest = (TextView) findViewById(R.id.test);
tvTest.setTextSize(30f);
tvTest.setText(R.string.one_line);
btnChangeSize = (Button) findViewById(R.id.btn_changeSize);
btnChangeSize.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(sizeChanged){
sizeChanged = false;
tvTest.setTextSize(30f);
}else{
sizeChanged = true;
tvTest.setTextSize(18f);
}
}
});
btnChangeText = (Button) findViewById(R.id.btn_changeTest);
btnChangeText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(textChanged){
textChanged = false;
tvTest.setText(R.string.one_line);
}else{
textChanged = true;
tvTest.setText(R.string.two_line);
}
}
});
}
}求指教!!
当TextView的文字显示为两行时候就不存在这个问题。这时候缩小文字的大小,View的高度也缩小。为了证实这个问题,我特意写里个Demo如下:1.字符串资源:<string name="one_line">AB</string>
<string name="two_line">AB\nCD</string>2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF00"/>
<Button
android:id="@+id/btn_changeSize"
android:text="ChangeSize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Button>
<Button
android:id="@+id/btn_changeTest"
android:text="ChangeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Button></LinearLayout>3.TestActivity.javapublic class TestActivity extends Activity {
TextView tvTest;
Button btnChangeSize;
Button btnChangeText;
boolean sizeChanged;
boolean textChanged;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvTest = (TextView) findViewById(R.id.test);
tvTest.setTextSize(30f);
tvTest.setText(R.string.one_line);
btnChangeSize = (Button) findViewById(R.id.btn_changeSize);
btnChangeSize.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(sizeChanged){
sizeChanged = false;
tvTest.setTextSize(30f);
}else{
sizeChanged = true;
tvTest.setTextSize(18f);
}
}
});
btnChangeText = (Button) findViewById(R.id.btn_changeTest);
btnChangeText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(textChanged){
textChanged = false;
tvTest.setText(R.string.one_line);
}else{
textChanged = true;
tvTest.setText(R.string.two_line);
}
}
});
}
}求指教!!
http://stackoverflow.com/questions/9541196/androidtextview-height-doesnt-change-after-shrinking-the-font-size