代码之家  ›  专栏  ›  技术社区  ›  Bob Joe

在工具栏中居中放置Android Drawable XML徽标

  •  1
  • Bob Joe  · 技术社区  · 10 年前

    我正试图在工具栏中放置一个android可绘制的XML资源。工具栏是从android studio 1.5中的导航抽屉活动模板生成的。

    例子:

    我得到的: enter image description here

    我的目标是:

    enter image description here

    我当前的代码将资源垂直集中,而不是水平集中,我已经到处寻找资源集中的方法,但是我没有找到解决方案。我还尝试将资源文件作为与XML代码相关的工具栏中的图像视图居中,但是,图像居中,但位于工具栏底部(水平居中,而不是垂直居中)。

    当前代码: 导航抽屉活动

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_navigation_drawer);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(null);
            toolbar.setLogo(R.drawable.app_logo);
    

    app_bar_navigation_drawer.xml ( 违约 )

        <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    </android.support.design.widget.AppBarLayout>
    

    非常感谢您的帮助!

    1 回复  |  直到 10 年前
        1
  •  1
  •   timr    10 年前

    您可以编辑工具栏,因为它是从 ViewGroup 像这样的事情。

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    
        <ImageView
            android:layout_width="wrap_content"
            android:contentDescription="@string/logo"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"/>
    
     </android.support.v7.widget.Toolbar>