Android centre title with navigation drawer

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I currently have a navigation drawer with a toolbar that has a title, i wish to centre this title within the toolbar but the toolbar does not seem to take into consideration the drawer icon as you can see in the following picture.

whereas when i use the same toolbar layout for other activities that are not inside the navigation drawer the title is centred perfectly, as show in this picture:

So how do i get it to take into account this icon?

Here is my layout:

回答1:

You need to understand that Toolbar widget extends ViewGroup class, and it has it's own LayoutParams.

So you do not require RelativeLayout inside Toolbar, and need to add just single line with TextView.

android:layout_gravity="center_horizontal" 

And final xml should look like this,



回答2:

Remove the RelativeLayout, then change your TextView like this:

After that call setDisplayShowTitleEnabled, to remove the original title, in the onCreate method of your activity:

public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);         setSupportActionBar(toolbar);         getSupportActionBar().setDisplayShowTitleEnabled(false);          ...     } } 

This is the result obtained:



回答3:

You need to change android:layout_width="wrap_content" to android:layout_width="match_parent" in Textview.....



回答4:

Hope this will help. try to use this code into your xml , your code with few changes :

Use this for each activity. This will makes sure that your text would cover the entire space next to the icon and center the text within it.

  --------Nav_Drawer layout       


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!