如何将活动的标题放在工具栏的右侧

时间:2016-07-12 17:32:02

标签: android android-studio

我编写了一个代码来将活动的标题转移到右边。以下是java代码。我使用的是getSupportActionBar()。

这是Image of the toolbar I want

   package com.example.hsports.practicesession;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

public class HomePage extends AppCompatActivity {

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

        setContentView(R.layout.activity_home_page);

        String getUserEmail = getIntent().getExtras().getString("userEmail");
        String getUserPassword = getIntent().getExtras().getString("userPassword");


        TextView displayUserEmail= (TextView) findViewById(R.id.displayUserEmail);
        //this.setTitle("Welcome"+" "+getUserEmail);

       // getActionBar().setTitle("Welcome"+" "+getUserEmail);




        TextView displayUserPassword=(TextView)findViewById(R.id.displayUserPassword);


        displayUserEmail.setText(getUserEmail);
        displayUserPassword.setText(getUserPassword);







        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        TextView welcomeTextView = (TextView) toolbar.findViewById(R.id.welcomeText);
        welcomeTextView.setText("Welcome " + getUserEmail);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

这是主页的xml代码。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.hsports.practicesession.HomePage"
    tools:showIn="@layout/activity_home_page"

    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"

        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"

            />


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/welcomeText"
                android:layout_marginRight="30dp"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

    </android.support.design.widget.AppBarLayout>



    <TextView
        android:id="@+id/displayUserEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/displayUserPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

我实际上希望页面标题位于上面操作栏的右侧。 怎么做。

我得到的错误是:

07-14 00:03:03.763 3038-3038 / com.example.hsports.practicesession E / AndroidRuntime:FATAL EXCEPTION:main                                                                                处理:com.example.hsports.practicesession,PID:3038                                                                                java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.hsports.practicesession / com.example.hsports.practicesession.HomePage}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView。 null对象引用上的setText(java.lang.CharSequence)'                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                    在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                                 引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                                                                                    在com.example.hsports.practicesession.HomePage.onCreate(HomePage.java:46)                                                                                    在android.app.Activity.performCreate(Activity.java:6237)                                                                                    在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                    在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:0)

空对象引用是因为您在设置工具栏本身之前设置工具栏的标题。这可以通过以下方式解决:

    //REMOVED
    TextView displayUserPassword=(TextView)findViewById(R.id.displayUserPassword);


    displayUserEmail.setText(getUserEmail);
    displayUserPassword.setText(getUserPassword);



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    this.getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);

答案 1 :(得分:0)

你的

 this.getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);

正在尝试设置不存在的操作栏的标题。

您的应用主题可能没有操作栏,或者您没有正确引用它。

答案 2 :(得分:0)

您的xml布局中应该有一个工具栏,因此您的xml文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.hsports.practicesession.HomePage"
    tools:showIn="@layout/activity_home_page">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <TextView
        android:id="@+id/displayUserEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/displayUserPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

然后您需要在supportActionBar方法中设置onCreate,如下所示:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

之后你可以设置标题:

getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);

但在您的情况下,您在设置supportActionBar之前尝试设置标题,因此您获得了NullPointerException

此外,R.id.toolbar不在您当前的xml布局中,因此它再次抛出NullPointerException,因此请按照上面的建议更改您的xml文件以添加Toolbar

<强>更新

将文字放在右边:

您需要在TextView中添加自己的Toolbar,然后无需像这样设置工具栏标题:

getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);

将您的activity_home_page.xml代码更改为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.hsports.practicesession.HomePage">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/welcomeText"
                    android:layout_marginRight="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </RelativeLayout>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_home_page" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

然后在Activity执行此操作:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView welcomeTextView = (TextView) toolbar.findViewById(R.id.welcomeText);
welcomeTextView.setText("Welcome " + getUserEmail);

输出

enter image description here