Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textview alignment issue when changing font/text size

I have looked through quite a few answers and questions here on stackoverflow but have not found anything really helpful regarding my problem.

I have two textviews of equal size and equal properties right next to each other, the xml layout is this (only textsize differ)

<?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" />

and the code is this

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;

public class MainActivity extends 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;
}

}

Why is the height of the 'white space' below the numbers changing with text/font size? (se image below)

How can I determine if the problem has something to do with the font or with the textview?

I'm using this font: [http://www.dafont.com/ww-digital.font][3]

enter image description here

like image 725
niaren Avatar asked Nov 30 '25 14:11

niaren


1 Answers

Use the same font ie ttf for both text view and use android:ems in Textview to define max and min value of it.

like image 117
user2007354 Avatar answered Dec 06 '25 21:12

user2007354