2016-09-29 113 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> 

我仍然建议减小标志的大小,因为它可能会导致未来的内存泄漏。