xml - Android center textview in custom ActionBar using a linearlayout horizontally and vertically -
i'm trying since several hours center title in action bar horizontally. code:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/actionbar" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false" android:layout_gravity="center" android:gravity="center"> <textview android:id="@+id/activitytitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textsize="20sp" android:textcolor="#ffffff" /> </linearlayout>
and oncreate()
code in activity
:
this.getactionbar().setdisplayshowcustomenabled(true); this.getactionbar().setdisplayshowtitleenabled(false); layoutinflater inflator = layoutinflater.from(this); view v = inflator.inflate(r.layout.custom_action_bar, null); typeface tf = typeface.createfromasset(getassets(),"font/myfont.ttf"); ((textview)v.findviewbyid(r.id.activitytitle)).settypeface(tf); ((textview)v.findviewbyid(r.id.activitytitle)).settext("test"); getactionbar().setcustomview(v); getactionbar().setdisplayshowhomeenabled(false);
the actionbar icon hided , title in correct font, centered vertically, not horizontally. how can that?
you use relativelayout
, properties
android:layout_centerhorizontal="true"
and
android:layout_centervertical="true"
for example:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/actionbar" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/activitytitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="jorgesys here!" android:textcolor="#ffffff" android:textsize="20sp" /> </relativelayout>
Comments
Post a Comment