android:set title bar in middle

让人想犯罪 __ 提交于 2019-12-06 08:42:59

I can find the real answer, at last i load the new layout for center the title bar set title bar code in Oncreate

 final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.title_bar);
        }
        final TextView myTitleText = (TextView) findViewById(R.id.title_bar);
        if (myTitleText != null) {
            myTitleText.setText(R.string.title_ble_melem);

        }

here is the layout

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    >
    <TextView
        android:id="@+id/title_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="title bar"
        android:layout_centerInParent="true"
        android:layout_marginLeft="5dip"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:textSize="20sp"
       />

     <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/title_bar"
        android:src="@drawable/ic_top_bar"

    />
    </RelativeLayout >

Thank you very much for response.

necixy

just put the code under the setContentView(R.layout.new)...

((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);

To be very frank this code is copied from another stackoverflow question: How to align center the title or label in activity?

I am sure it can help you mate!!!!

;)

This page can help you?

This:

<TextView android:id="@android:id/title" 
   style="?android:attr/windowTitleStyle"
   android:background="@null"
   android:fadingEdge="horizontal"
   android:gravity="center_vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

And:

ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
LinearLayout root = (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
TextView title = (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);

This should be called in the Activity.onCreate(...) after setting the content view. From source.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!