简体   繁体   English

在Android中更改标题栏文本无效

[英]Change title bar text in Android doesn't work

I'm trying to customize the title bar in my android application. 我正在尝试在我的android应用程序中自定义标题栏。 In order to do that, I used accepted answer of this question. 为了做到这一点,我使用了这个问题的公认答案。

Here is the code I used. 这是我使用的代码。

Here is my titlebar.xml 这是我的titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView android:id="@+id/ImageView01"
        android:layout_width="57dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_prof"/>

    <TextView

        android:id="@+id/myTitle"
        android:text="This is my new title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#FFA500"
        />
</LinearLayout>

Here is my main.xml 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

Here is my TitleBar.java 这是我的TitleBar.java

package com.myayubo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

public class TitleBar extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
        }

        final TextView myTitleText = (TextView) findViewById(R.id.myTitle);

        if (myTitleText != null) {
            myTitleText.setText("NEW TITLE");
            // user can also set color using "Color" and then
            // "Color value constant"
             myTitleText.setBackgroundColor(Color.GREEN);
        }
    }
}

Here is my strings.xml 这是我的strings.xml

 <string name="hello_world">Hello world!</string>
    <string name="app_name">My Ayubo</string>
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>

But, I cannot customize my title bar using following code. 但是,我无法使用以下代码来自定义标题栏。 I mean, code is running without any errors. 我的意思是,代码正在运行,没有任何错误。 But, it is not doing what I expected. 但是,它没有达到我的预期。 What I need is to add an image and a title in to my header. 我需要在标题中添加图片和标题。 How am I suppose to do that? 我应该怎么做?

figured it out in my own. 我自己弄明白了。

First added following items in my menu.xml 首先在我的menu.xml中添加了以下项目

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="1"
        android:showAsAction="never" />

    <item
        android:id="@+id/action_example"
        android:title="Logout"
        android:orderInCategory="2"
        android:showAsAction="withText|ifRoom" />

    <item
        android:id="@+id/profPicture"
        android:title=""
        android:orderInCategory="3"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_prof"
        android:onClick="userProfile"
        />

    <item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>

    <item
        android:id="@+id/addLoc"
        android:title=""
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_location"
        android:onClick="gotoLocation"/>

    <item
        android:id="@+id/namePerson"
        android:title="Nathalia Smith"
        android:orderInCategory="4"
        app:showAsAction="ifRoom"
        android:onClick="userProfile"/>
</menu>

Then added following methods to java file to implement onlick events of images and buttons. 然后将以下方法添加到java文件中,以实现图像和按钮的onlick事件。

  public void doThis(MenuItem item){
       // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), DealsList.class);
        finish();
        startActivity(intent);
    }

    public void gotoLocation(MenuItem item){
        // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), AddLocation.class);
        finish();
        startActivity(intent);
    }

    public void userProfile(MenuItem item){
        // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), UserProfile.class);
        finish();
        startActivity(intent);
    }

Thanks for everyone who answered. 感谢所有回答的人。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM