android:set title bar in middle

混江龙づ霸主 提交于 2019-12-08 01:24:19

问题


I create the custom title using the style.I code some from http://labs.makemachine.net/2010/03/custom-android-window-title/

I have also set the icon in title bar using the setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.title_logo); can it is possible to set the title bar in middle?if possible then I want set using the custom style.

EDIT: i have check code http://andmobidev.blogspot.com/2010/01/centering-title-of-window.html it work fine when there no icon in title bar but when set the icon i get the error java.lang.ClassCastException: android.widget.RelativeLayout i have no idea how the window draw the title bar ,i have check the one of blog http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html

Thank you.


回答1:


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.




回答2:


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!!!!

;)




回答3:


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.



来源:https://stackoverflow.com/questions/6067490/androidset-title-bar-in-middle

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