Android 工具栏自定义文本未显示

Android Toolbar Custom Text not showing

我正在尝试在首页工具栏的右上角显示用户名 page.I 在 xml 页面的工具栏内放置了一个文本视图。当我从登录页面通过 google 登录按钮登录时显示文本,而在手动登录时不显示文本。我需要一些帮助...

xml 页数:

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Toolbar title"
                android:gravity="center"
                android:layout_gravity="end"
                android:paddingRight="5dp"
                android:id="@+id/toolbar_title" />

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

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

HomeActivity.java :

 Toolbar toolbar;
 TextView txtCategory,txtService,toolbartext;
 String name;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Home");
    toolbar.setTitleTextAppearance(this, R.style.ToolBarStyle);
     toolbarview = findViewById(R.id.toolbar);
    toolbartext = (TextView) toolbar.findViewById(R.id.toolbar_title);

      String username = getIntent().getStringExtra("username");
     if(LoginActivity.isValidEmail(username))
    {
      name= databaseHelper.selectmailuser(username);
      if(name==null)
        Toast.makeText(this,"Email not Registered",Toast.LENGTH_LONG).show();

        Toast.makeText(this,name,Toast.LENGTH_LONG).show(); 
        //value is correct shown in toast
      toolbartext.setText(name);   // not getting in toolbar

    }

    else if(LoginActivity.Phone_Validate(username))
    {
        String name=  databaseHelper.selectphoneuser(username);
        if(name==null)
            Toast.makeText(this,"Mobile Number not Registered",Toast.LENGTH_LONG).show();

        toolbartext.setText(name);    // value not getting in toolbar
    }

    String googlename = getIntent().getStringExtra("googlename");
    toolbartext.setText(googlename);   //here only i am getting  value

你的xml没问题。

试试这个代码:

Toolbar toolbar;
TextView textViewTitleToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Objects.requireNonNull(getSupportActionBar()).hide();
    toolbar = findViewById(R.id.toolbar);
    textViewTitleToolbar= findViewById(R.id.textViewTitleToolbar);
    textViewTitleToolbar.setText("Home");
 }

当您想更改工具栏的文本时使用textViewTitleToolbar.setText(username);

您可以像这样更改工具栏标题。

 Textview toolbartext= findViewById(R.id.textViewTitleToolbar);
 toolbartext.setText("Your Text");

检查你的if条件可能是if条件没有执行

use this code 

this is MainActivity.java

public class MainActivity extends AppCompatActivity {
    String TAG="state";
    EditText editText;
    android.support.v7.widget.Toolbar toolbar;
    TextView toolbarname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbarname = findViewById(R.id.textview);

    }


    public void onclick(View view) {


        editText=findViewById(R.id.editText1);
        String name =editText.getText().toString();
        toolbar=(android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);



        if(name==null)
            Toast.makeText(this,"Email not Registered",Toast.LENGTH_LONG).show();

        Toast.makeText(this,name,Toast.LENGTH_LONG).show();
        //value is correct shown in toast
        toolbarname.setText(name);
        setSupportActionBar(toolbar);
    }
}

//--------------------------------------------------------

this is activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView


                android:gravity="right"
                android:text="name"
                android:id="@+id/textview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="?attr/colorBackgroundFloating"
                android:textSize="16dp" />
        </LinearLayout>

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




    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:width="200dp"
        android:inputType="text"
        android:saveEnabled="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.356" />

    <Button
        android:onClick="newclick"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="280dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.285" />

</RelativeLayout>

通过编程:

Toolbar toolbar  toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Your title);
toolbar.setTitleTextColor("title color");
toolbar.setSubtitle("Your subtitle");
toolbar.setSubtitleTextColor("subitle color");