2017-05-23 1 views
2

Activity에서 비정상적으로 나타나는 메뉴를 고려하여 툴바에 텍스트를 가운데 맞추려고합니다. 나는 많은 해결책을 찾았지만 메뉴가 팽창 된 경우를 다루지 않는 것을 찾지 못했습니다.Android - 메뉴가 비정상적으로 툴바에 표시되는 이미지

menu을 팽창시키지 않고 ImageView 만 팽창 시키면 모든 것이 정상입니다. 나는 ImageView 다음 imageToolbar의 중앙에 표시되지 않는 menu하지만, 센터 자체 공간 (햄버거와 메뉴 사이를) 팽창 경우

결론 :

내가 필요 센터 ImageViewToolbar, 나중에 뭔가 부 풀리거나 상관없이. 나는 순간에 팽창 오전 menu는 하나의 item (가시 항상 스위치) 여기

을 가지고 내 현재 코드 :

<android.support.design.widget.AppBarLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       app:elevation="0dp"> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:background="?attr/colorPrimary" 
        app:contentInsetLeft="0dp" 
        app:contentInsetStartWithNavigation="0dp" 
        app:theme="@style/ToolbarTheme"> 

        <ImageView 
         android:id="@+id/toolbar_logo" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:src="@drawable/app_logo" /> 

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

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

활동 :

...  
@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main_toolbar_user_visibility, menu); 
     return true; 
    } 
... 

어떻게 할 수 메뉴가 팽창 된 후에도 ImageView를 중앙에 배치 할 수 있습니까?

답변

6

나는 (햄버거와 메뉴 사이) 자신의 공간 에 이미지 뷰 다음 이미지가 도구 모음의 중앙에 표시되지 않는 메뉴,하지만 중심을 팽창합니다.

이 때문에 일어나고는, 당신은 Toolbar 내부 및 ToolbarActionBar으로 사용으로 ImageView을 추가 한, 그래서 back 또는 drawer 아이콘 및 옵션 menu에 대한 right 공간을 일부 left 공간을 사용합니다.

Toolbar = "Back arrow" + "Middle area(Title/Image)" + "Option menu" 

해결책 :

1AppBarLayout의 직접적인 아이로서 RelativeLayout를 추가합니다.

2

RelativeLayout 내부 ToolbarImageView을 넣습니다. imageView

3. 추가 속성 android:layout_centerInParent="true"Toolbarcenter에 표시합니다.라인 아래 추가 활동에

<?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:id="@+id/mainCoordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     app:elevation="0dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:contentInsetLeft="0dp" 
       app:contentInsetStartWithNavigation="0dp" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

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

      <ImageView 
       android:id="@+id/toolbar_logo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:src="@mipmap/ic_launcher" /> 
     </RelativeLayout> 

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

    <!-- Your Content here --> 

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

:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

setSupportActionBar(toolbar); 
getSupportActionBar().setTitle(""); // hide title 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

출력 :

enter image description here

여기

는 작업 코드

희망이 도움이 될 것입니다 ~

+0

좋은 해결책과 좋은 설명! – Bugdr0id

+0

감사합니다 :) 내 대답이 유용하다고 생각되면 투표 할 수 있습니다. 미리 감사드립니다. – FAT

관련 문제