Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

layout - Toolbar in android

Is there any tutorial easy to understand to create a toolbar with the home button aligned to the left and the log out button aligned to the right?

I am using menu type .xml files to do it, but I am not able to align the buttons as I want.

My .xml is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/etxera"
        android:icon="@drawable/etxera_fondo"
        android:title="Hasierara"

        app:showAsAction="always" />

    <item android:id="@+id/saioa_itxi"
        android:icon="@drawable/saioaitxi"
        android:title="saioa itxi"
      app:showAsAction="ifRoom"  />
</menu>

Thanks for support!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make a custom toolbar

  1. Create a xml file

toolbar.xml

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:titleTextColor="#FFFFFF">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/left_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left" />
            <Button
                android:id="@+id/right_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right" />
        </LinearLayout>
</android.support.v7.widget.Toolbar>
  1. Include the toolbar into your main_activity.xml

     <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    
    </RelativeLayout>
    

Don't forget to set the NoActionBar theme


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...