如何在工具栏的中心制作徽标?

时间:2017-06-01 05:39:42

标签: android xml android-xml android-toolbar

我想在中心制作徽标,然后将其设置为左侧&不是大屏幕分辨率设备的中心。

下面是我的xmls

activity_app_base.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:elevation="1dp">


    <include
        layout="@layout/app_bar_app_base"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.design.widget.CoordinatorLayout>

app_bar_app_base.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <include layout="@layout/new_header_toolbar_design"></include>

    <include layout="@layout/content_app_base" />

</LinearLayout>

new_header_toolbar_design.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:contentInsetStart="0dp"
            app:theme="@style/MyToolbarStyle"
            android:contentInsetLeft="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:contentInsetStartWithNavigation="0dp"
            app:contentInsetStartWithNavigation="0dp">

            <RelativeLayout
                android:id="@+id/rl_User_Profile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageView_User_Image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:visibility="gone"
                    android:layout_margin="1dp" />
                <ImageView
                    android:id="@+id/imageView_APP_LOGO"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="invisible"
                    android:layout_alignRight="@+id/imageView_User_Image"
                    android:src="@drawable/header_logo"
                    android:layout_centerVertical="true"
                    android:layout_margin="1dp" />
                <ImageView
                    android:id="@+id/imageView_LOGO"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_alignTop="@+id/imageView_APP_LOGO"
                    android:layout_toRightOf="@+id/imageView_APP_LOGO"
                    android:layout_toLeftOf="@+id/imageView_Space"
                    android:layout_alignBottom="@+id/imageView_APP_LOGO"
                    android:paddingLeft="-65dp"
                    android:src="@drawable/header_logo"
                    />
                <ImageView
                    android:id="@+id/imageView_Space"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    />



            </RelativeLayout>


        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

3 个答案:

答案 0 :(得分:0)

对ImageView使用android:layout_centerInParent="true"并删除

                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"

答案 1 :(得分:0)

要在中心对齐徽标,我在相应的Activity中使用此代码..

此代码可以帮助您

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setLogo(R.drawable.logo_title);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

答案 2 :(得分:0)

使用“线性布局”并将“线性布局”的重力设置为居中

<?xml version="1.0" encoding="utf-8"?>
            <android.support.design.widget.AppBarLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/white"
                    android:contentInsetStart="0dp"
                    app:theme="@style/MyToolbarStyle"
                    android:contentInsetLeft="0dp"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    android:contentInsetStartWithNavigation="0dp"
                    app:contentInsetStartWithNavigation="0dp">

                    <LinearLayout
                        android:id="@+id/rl_User_Profile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:orientation="horizontal">

                  <!--Change the size of imageview as per your requirement-->
                        <ImageView
                            android:id="@+id/imageView_User_Image"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:visibility="gone"
                            android:layout_margin="1dp" />
                        <ImageView
                            android:id="@+id/imageView_APP_LOGO"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:visibility="invisible"
                            android:src="@drawable/header_logo"
                            android:layout_margin="1dp" />
                        <ImageView
                            android:id="@+id/imageView_LOGO"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/header_logo"
                            />
                        <ImageView
                            android:id="@+id/imageView_Space"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            />
                    </LinearLayout>

                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.AppBarLayout>