Center a textview

0

I have this layout in my application:

AndI'mcreatingitlikethis:

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

They are separated by 3% with% where I add this:

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'm wanting is: Is it possible to center the text underneath the photo?

The setting of LinearLayout :

<?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"/>
    
asked by anonymous 26.02.2015 / 15:12

1 answer

3

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

Looking like this:

<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" />
    
26.02.2015 / 17:28