【问题标题】:android:set title bar in middleandroid:在中间设置标题栏
【发布时间】:2011-05-20 04:32:40
【问题描述】:

我使用样式创建自定义标题。我从http://labs.makemachine.net/2010/03/custom-android-window-title/ 编写了一些代码

我还使用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

谢谢。

【问题讨论】:

    标签: android android-titlebar


    【解决方案1】:

    我可以找到真正的答案,最后我加载了标题栏居中的新布局 在 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 >
    

    非常感谢您的回复。

    【讨论】:

    • +1 用于发布不依赖于未记录结构的正确答案。但是,如果您想在自定义标题栏中添加 progress bar,您将无法做到。
    【解决方案2】:

    只需将代码放在 setContentView(R.layout.new) 下...

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

    坦率地说,这段代码是从另一个 stackoverflow 问题复制而来的:How to align center the title or label in activity?

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

    ;)

    【讨论】:

    • 感谢您的回复,但我收到错误原因:java.lang.ClassCastException: android.widget.RelativeLayout com.vvseksperten.HomeScreenActivity.onCreate(HomeScreenActivity.java:28) 可能是由于我的布局请在这里查看pastebin.com/VffL6Mng
    • 嗨,当只有 textView 时我没有错误,但是当我在标题栏中设置图标时,它给出了错误
    【解决方案3】:

    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(...) 中调用。 来源

    【讨论】:

    • “点击这里”这样的答案在 SO 上不受支持/建议。
    • 谢谢!那是在我明白什么是 SO 之前。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多