2016-09-29 74 views
0

我試圖在主要活動的應用程序工具欄的中心設置徽標,但由於徽標位於高清,因此導致工具欄增加了大小以容納標誌的大小。有沒有一種方法可以按比例減少徽標的大小,以便在工具欄上正確設置?使用工具欄的大小在Android工具欄上設置徽標

這裏是我的代碼:

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

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      app:titleTextColor="@android:color/white"></android.support.v7.widget.Toolbar> 

<style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar"> 
     <item name="colorControlNormal">@color/white</item> 
    </style> 

這裏是結果:

Toolbar

+0

您應該在工具欄中設置一個ImageView,然後您可以將其設置爲您的優先大小。 –

+0

請勿加載比您需要的更大的位圖。不要浪費記憶。製作較小版本的徽標。 –

回答

1

工具欄在Android是一個ViewGroup中。因此,您可以將工具欄內的小部件作爲其子項。同時爲了使日誌成爲工具欄的大小,給工具欄一個確定的高度並將圖像大小設置爲match_parent。例如:

<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/black"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_clock" 
     android:layout_gravity="center_horizontal" 
     /> 
    </FrameLayout> 
</android.support.v7.widget.Toolbar> 

我仍然建議減小標誌的大小,因爲它可能會導致未來的內存泄漏。