【问题标题】:Set title text on custom title bar在自定义标题栏上设置标题文本
【发布时间】:2013-11-20 12:09:35
【问题描述】:

我必须创建一个自定义标题栏,以设置动态文本。这类似于在操作栏上设置标题,以及下面的动态字幕。

在这种情况下,App 标题默认位于左侧。动态文本位于右侧,并且必须动态更改。

这是布局 custom_titlebar.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="vertical" >

<TextView 
    android:id="@+id/title_left_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="left" />

<TextView 
    android:id="@+id/title_right_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="right"
    android:layout_alignParentRight="true" />    
</RelativeLayout>

在我定义的清单中:

<application
    android:theme="@style/Theme.NoTitle" >

Activity上:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //HAVE COMENTED THIS, BECAUSE IT THROWS: You cannot combine custom titles with other title feature..

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar);

    TextView title = (TextView) findViewById(R.id.title_left_text);
    title.setText(getTitle());

要动态更改文本,我还有Activity

public void setTitleStatus(String right) {
    if (right.length() > 20) {
        right = right.substring(0, 20);
    }
    TextView status = (TextView) findViewById(R.id.title_right_text);

    status.setText(right);
}

问题是在我设置标题的 onCreate 行上抛出了一个 NPE (title.setText(getTitle());)

【问题讨论】:

    标签: android titlebar custom-titlebar


    【解决方案1】:

    改变

    title.setText(getTitle().toString());
    

    【讨论】:

    • 如果这是您自己的 getTitle() 方法吗?
    • 我没有 getTitle() 方法。我想这是一个预定义的方法。在那里设置应用标题的方法是什么?
    • UPDATE-> 我尝试设置 title.setText("TITLE") 但继续抛出 NPE
    • 并非如此。在定义样式时给我带来了问题。如果我在主题中设置:@android.style/Theme.black 它是正确的。但如果我设置 Holo 主题然后出现在 logcat 上:You cannot combine custom titles with other title feature
    • 问题是getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar);这里
    【解决方案2】:

    查看 Android 示例。在蓝牙项目中制作了一个自定义操作栏,在不同的场合改变它的状态

    【讨论】:

    • 是的,我现在就是这样。但我需要它没有 ActionBar
    • 为什么需要没有 ActionBar ?如果您有一个很好的示例,并且您的想法是在活动顶部显示标题,为什么不使用此方法?
    猜你喜欢
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多