Centralize a textview

Asked

Viewed 14,680 times

1

I have this layout in my app:

layout

And I’m raising him like this:

TextView txtCategoria = (TextView)LayoutInflater.from(this).inflate(R.layout.text, null);               
txtCategoria.setText(listenerCategoria.nome);
final ImageButton btCategoria = (ImageButton) LayoutInflater.from(this).inflate(R.layout.button, null);
btCategoria.setId((int) listenerCategoria.id);
btCategoria.setImageResource(getResources().getIdentifier(listenerCategoria.nome_foto, "drawable", getPackageName()));

Are separated into 3 LinearLayout where I add so:

if(l==1){
     layout1.addView(btCategoria);
     layout1.addView(txtCategoria);
     Log.e("l1","entrou "+txtCategoria.getText());
     l=2;
 }else if(l==2){
     layout2.addView(btCategoria);
     layout2.addView(txtCategoria);
     Log.e("l2","entrou "+txtCategoria.getText());
     l=3;
 }else if(l==3){
     layout3.addView(btCategoria);
     layout3.addView(txtCategoria);
     Log.e("l3","entrou "+txtCategoria.getText());
     l=1;
  }

What I want is: It is possible to center the text under the photo?

The configuration of text.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txtCategoria"/>

1 answer

4


You need to change the property layout_width to occupy all parent content and add gravity to center the text.

Thus remaining:

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/txtCategoria" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.