可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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