2011-05-20 77 views
2

设置标题栏我从http://labs.makemachine.net/2010/03/custom-android-window-title/安卓:在中间

创建使用style.I代码一些自定义标题我还设置使用标题栏中的图标setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.title_logo); 可它可以设置标题栏在中间?如果可能的话,我想使用自定义样式进行设置。

编辑:我有校验码http://andmobidev.blogspot.com/2010/01/centering-title-of-window.html 它正常工作时,有标题栏没有图标,但设置时,我得到的错误java.lang.ClassCastException: android.widget.RelativeLayout 我不知道窗口如何绘制标题栏的图标,我有检查一个的博客 http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html

谢谢。

回答

5

我能找到真正的答案,最后我加载新的布局中心的标题栏 组标题栏的代码的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); 

     } 

这里是布局

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

非常感谢您的回复。

+0

+1发布一个正确的答案,不依赖于无证结构。但是,如果您想添加到自定义标题栏的[进度条](http://stackoverflow.com/q/11254366/869501),您将无法使用。 – ateiob 2012-06-29 12:32:49

2

This page可以帮到你吗?

此:

<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" /> 

和:

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); 

这应该在Activity.onCreate(...)设置内容视图后调用。 来自源

+0

“点击此处”在SO上不支持/建议这样的回答。 – Nezam 2014-04-14 07:09:27

+0

谢谢!在我明白什么是SO之前。 :) – 2014-04-14 19:06:37

+0

(y)投票up.as奖励 – Nezam 2014-04-14 23:22:06

2

只是把代码中的setContentView(R.layout.new)下...

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

坦白地说这个代码是从另一个计算器的问题复制:How to align center the title or label in activity?

我相信它可以帮助你交配!

;)

+0

感谢您的回应,但我得到的错误引起:java.lang.ClassCastException:android.widget.RelativeLayout com.vvseksperten.HomeScreenActivity.onCreate(HomeScreenActivity.java:28)可能由于我的布局,请检查它在这里http://pastebin.com/VffL6Mng – 2011-05-20 06:44:17

+0

嗨,我没有错误,当只有textView,但当我在标题栏中设置图标它给出错误 – 2011-05-20 08:23:28