更改字体/文本大小时的Textview对齐问题

时间:2013-01-24 12:48:15

标签: android fonts textview alignment

我已经在stackoverflow上查看了很多答案和问题但是没有找到任何关于我的问题真正有用的东西。

我有两个相同大小和相同属性的文本视图彼此相邻,xml布局是这样的(只有文本大小不同)

<?xml version="1.0" encoding="utf-8"?>

    <TextView
        android:id="@+id/data_content_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".50"
        android:gravity="bottom|center"
        android:text="201.5"
        android:textSize="50sp"
        android:textStyle="bold"
        android:singleLine="true"
        android:ems="62" />

    <TextView
        android:id="@+id/data_content_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.50"
        android:ems="62"
        android:gravity="center|bottom"
        android:singleLine="true"
        android:text="201.5"
        android:textSize="20sp"
        android:textStyle="bold" />

,代码就是这个

package com.example.fonttest; import android.os.Bundle; import android.app.Activity; import android.graphics.Typeface; import android.view.Menu; import android.widget.LinearLayout; import android.widget.TextView;

公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TextView textViewLeft  = (TextView) findViewById(R.id.data_content_left);
    TextView textViewRight = (TextView) findViewById(R.id.data_content_right);

    Typeface myTypeface = Typeface.createFromAsset(this.getAssets(), "fonts/wwDigital.ttf");

    textViewLeft.setTypeface(myTypeface, Typeface.NORMAL);
    textViewRight.setTypeface(myTypeface, Typeface.NORMAL);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

为什么数字下方的“空白区域”的高度会随着文字/字体大小的变化而变化? (见下图)

如何确定问题是否与字体或textview有关?

我正在使用这种字体:[http://www.dafont.com/ww-digital.font][3]

enter image description here

1 个答案:

答案 0 :(得分:1)

对于文本视图使用相同的字体,即 ttf ,并在Textview中使用android:ems来定义它的最大值和最小值。

相关问题