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

时间:2018-07-13 09:18:45

标签: android toolbar android-toolbar

我正在尝试在首页工具栏的右上角显示用户名。我已在xml页面的工具栏中放置了一个textview。当我从登录页面通过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

4 个答案:

答案 0 :(得分:0)

您的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);

答案 1 :(得分:0)

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

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

检查条件是否为条件

答案 2 :(得分:0)

data['flag'] = np.where(rule6 & ~rule2 & ~rule1,  'yes', data['flag'])

答案 3 :(得分:0)

通过编程方式:

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