how to center icons in toolbar in android

后端 未结 7 2027
清歌不尽
清歌不尽 2021-02-07 21:29

I asked a similar question here... I got some tutorials in the answers. But this question is diffrenet. because none of that method do not works in my project.

I want

相关标签:
7条回答
  • 2021-02-07 22:00

    The toolbar is like any other view. You can add children to it directly and access then like you would for a normal view.

    This is not the exact the answer to your question, but it will tell you the way to go.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar   xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/holo_red_light"
        android:elevation="2dp"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button1"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button2"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button3"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button4" />
    
        </LinearLayout>
    
    </android.support.v7.widget.Toolbar>
    

    Then in your main fragment or where ever else you can access something like this.

        Toolbar mToolbar = (Toolbar) root.findViewById(R.id.tool_bar);
    
        Button button1 = (Button) mToolbar.findViewById(R.id.button1);
        Button button2 = (Button) mToolbar.findViewById(R.id.button2);
        Button button3 = (Button) mToolbar.findViewById(R.id.button3);
        Button button4 = (Button) mToolbar.findViewById(R.id.button4);
    

    You can use this method to make a custom toolbar any way you like. You will probably want to use a relative layout.

    0 讨论(0)
提交回复
热议问题