【问题标题】:Aligning icon in toolbar工具栏中的对齐图标
【发布时间】:2016-11-30 05:08:42
【问题描述】:

如何将工具栏中的图标居中对齐?我已经删除了带有getSupportActionBar().setDisplayShowTitleEnabled(false); 的标签 为我的工具栏创建一个 xml 文件是最好的主意吗?

【问题讨论】:

    标签: android android-layout alignment android-toolbar


    【解决方案1】:

    是的,在工具栏中创建一些布局,在布局中您可以拥有其他控件(视图)。

    工具栏就像其他布局一样,您可以嵌套它们。

    <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar_actionbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          app:layout_scrollFlags="scroll|enterAlways">
          <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize">
               <TextView
                     android:id="@+id/title"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentLeft="true"
                     android:layout_alignParentStart="true"
                     android:layout_centerVertical="true"
                     android:text="@string/news"/>
          </RelativeLayout>
      </Toolbar>
    

    【讨论】:

      【解决方案2】:

      您可以按以下方式进行编程:

      mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo);
                  if( mToolbar == null ){
                      return;
                  }
                  int toolbarWidth = mToolbar.getWidth();
                  int imageWidth = imageView.getWidth();
                  imageView.setX((toolbarWidth-imageWidth)/2);
              }
          });
      

      自定义您的 layout_toolbar.xml:

      <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"
              style="@style/Toolbar"
              android:layout_width="match_parent"
              android:layout_height="@dimen/toolbar_height"                                                                   
              android:background="@drawable/toolbar_background"
              android:focusable="false"                                
              app:titleTextAppearance="@style/AppTheme.Toolbar.Title">
              <ImageView
                  android:id="@+id/logo"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@drawable/toolbar_logo"/>
      
      </android.support.v7.widget.Toolbar>
      

      【讨论】:

        猜你喜欢
        • 2019-12-22
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多