如何在工具栏的中心制作徽标?
问题描述:
我想在中心制作标志,但是它被设置为不在大屏幕分辨率设备的中心&。如何在工具栏的中心制作徽标?
下面是我的个XML
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>
答
使用android:layout_centerInParent="true"
为ImageView的和删除
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
答
要在中心对齐的标志,我在相应的活动使用此代码..
此代码可以帮助你
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.logo_title);
getSupportActionBar().setDisplayUseLogoEnabled(true);
答
使用线性布局和设置线性布局的重心到中心
<?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>
我认为这对您有帮助.. https:// stackoverfl ow.com/questions/43735960/custom-component-toolbar-fragment-with-onoptionmenu-items –
发布快照。它看起来怎样? – Wizard