Título personalizado y setTitle en Android
Frecuentes
Visto 7,835 veces
0
I have create a simple custom title which is showing perfectly. However I am not sure what id to give my TextView in order for the setTitle method to work its magic.
My first choice was @android:id/title but that did not work.
Example I have defined a custom title layout in xml and added it to my activity.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:scaleType="centerInside"
android:onClick="home"
android:src="@drawable/logo" />
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
The TextView is now the "title" container. What I want is for the android:label value from the manifest to automatically be put in this new title TextView.
Ie. the default method setTitle must be adding this value to some defined view id - I just need that id.
2 Respuestas
2
setTitle sets the title of the activity, it has nothing to do with TextView's you need to use setText() to set the text of a TextView:
((TextView)findViewById(R.id.text_view_id)).setText("TextView text");
EDIT: Check this solution -> ¿Cómo configurar la barra de título personalizada TextView Value dinámicamente en Android?
contestado el 23 de mayo de 17 a las 11:05
1
I had to create a similar feature with a custom title. I highly doubt you will be able to do it by matching an id, that would be a huge security flaw if Android allowed that. What I did was extend Activity and then override the setTitle method in a fashion like...
TextView mCustomTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
mCustomTitle = (TextView)findViewById(R.id.title);
}
@Override
public void setTitle(CharSequence title) {
//you can override the other setTitle as well if you need it
mCustomTitle.setText(title);
}
contestado el 24 de mayo de 12 a las 09:05
This is more or less what I did. I included the title from the manifest also using getTitle. Ie. .setText(getTitle()); - Slott
Did you create it as a base class? I found that really took most of the hassle out of it? - Mike Israel
Yes I have a BaseActivity class that I extend from. It makes things easier but I still think it would be nice if the setTile could work with a custom title xml if the correct id was used for the testview holding the title. - Slott
@slott glad to hear it helped, if so please accept the answer. Also I am guessing that if they exposed that element it could lead to some huge security risks for malicious behaviors. - Mike Israel
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android title or haz tu propia pregunta.
Yes I know how to set the text of a text view. What I am asking for is an id to use in my custom title layout so that the setTitle method will still work. - Slott