android v7包里的Toolbar,怎么定制图标,字体居中的效果

 我来答
夜独行孤单
2016-04-19 · TA获得超过3458个赞
知道大有可为答主
回答量:2803
采纳率:82%
帮助的人:572万
展开全部
 首先使用 Toolbar 来代替ActionBar ,这样我们就能够把ActionBar嵌入到我们的View体系中,然后我们"禁用"系统的status bar,由 DrawerLayout 来处理status bar,最后抽屉部分往上移,或者裁剪掉status bar那一部分。
  控制Status bar
  在你的values-v21里面添加新的主题,并设置一下属性:
  values-v21/themes.xml
  <style name="AppTheme">
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
  </style>

  这里解释一下:
  windowDrawsSystemBarBackgrounds ,将它设置为true,系统将在你的window里面绘制status bar,默认为 TRUE ,之所以要写出来是因为你的theme有可能是继承过来的,确保为true。(在这里小插曲一下,因调试时,总以为注释了这段代码就以为是false,程序员思维害苦了我。另外从命名来看,Android把它称为system bar,可能是为了与能被我们处理的status bar区分开而做的改变。)
  statusBarColor 设置为透明是因为我们不再需要系统的status bar,因为我们无法控制它的位置,后面我们将交由 DrawerLayout 来处理。
  使用DrawerLayout
  首先,你的布局文件应该是和这个类似的:
  <android.support.v4.widget.DrawerLayout
  xmlns:android="url"
  android:id="@+id/my_drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">
  <!-- Your normal content view -->
  <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <!-- We use a Toolbar so that our drawer can be displayed
  in front of the action bar -->
  <android.support.v7.widget.Toolbar
  android:id="@+id/my_awesome_toolbar"
  android:layout_height="wrap_content"
  android:layout_width="match_parent"
  android:minHeight="?attr/actionBarSize"
  android:background="?attr/colorPrimary" />
  <!-- The rest of your content view -->
  </LinearLayout>
  <!-- The navigation drawer -->
  <ScrimInsetsFrameLayout xmlns:android="rul"
  xmlns:app="url"
  android:layout_width="304dp"
  android:layout_height="match_parent"
  android:layout_gravity="left"
  android:background="@android:color/white"
  android:elevation="10dp"
  android:fitsSystemWindows="true"
  app:insetForeground="#4000">
  <!-- Your drawer content -->
  </ScrimInsetsFrameLayout>
  </android.support.v4.widget.DrawerLayout>

  在这里布局里面我们用到了一个的开源类 ScrimInsetsFrameLayout ,它的主要作用就是利用 fitsSystemWindows 的回调方法 fitSystemWindows(Rect insets) 来获取status bar的大小,然后调整画布已达到去掉status bar的效果,所以我们需要在ScrimInsetsFrameLayout 下设置 fitsSystemWindows 为true。当然你也可以不使用这个类,而改用 layout_marginTop 属性来达到效果。
  insetForeground 这个属性是ScrimInsetsFrameLayout自带的,表示插入区域的前景色,我们设置为带透明的黑色#4000。别忘了使用这个属性需要添加如下代码到attrs.xml里:
  values/attrs.xml
  <declare-styleable name="ScrimInsetsView">
  <attr name="insetForeground" format="reference|color" />
  </declare-styleable>

  自此,我们已经实现了将DrawerLayout抽屉的那一部分显示在 Toolbar 和systembar(为了和下面的status bar区分,我们称为system bar)之间了,可是system bar的颜色被我们设置了透明,所以我们接下来要改变status bar的颜色。
  改变Status bar的颜色
  你可能已经注意到刚才的布局里面 DrawerLayout 的 fitsSystemWindows 属性设置了为true,这是因为我们要在代码里面使用了 DrawerLayout 设置status bar颜色的方法:
  // 在这里我们获取了主题暗色,并设置了status bar的颜色
  TypedValue typedValue = new TypedValue();
  getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
  int color = typedValue.data;
  // 注意setStatusBarBackgroundColor方法需要你将fitsSystemWindows设置为true才会生效
  DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
  drawerLayout.setStatusBarBackgroundColor(color);

  使用ToolBar来代替ActionBar

  在代码里面这样设置:
  Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
  setSupportActionBar(toolbar);
AiPPT
2024-09-04 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲、导入文档内容”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表... 点击进入详情页
本回答由AiPPT提供
丿占戈灬龙哥
2016-04-19 · 知道合伙人软件行家
丿占戈灬龙哥
知道合伙人软件行家
采纳数:479 获赞数:1173

向TA提问 私信TA
展开全部
(1)在你的Toolbar里添加这样的属性:

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

app:popupTheme——
有时候我们有需求:
ActionBar文字是白的,ActionBar Overflow弹出的是白底黑字
让ActionBar文字是白的,那么对应的theme肯定是Dark。
可是让ActionBar弹出的是白底黑字,那么需要Light主题。
这时候popupTheme就派上用场了。

android:theme 与 app:theme——
在AppCompat v21里,提供了一个快速方便的方法设置Toolbar的主题,使用app:theme。
而新版本22.1.x中,AppCompat 允许对 Toolbar 使用android:theme代替app:theme。
最好的一点是:它会自动继承父视图的theme ,并且兼容所有APIv11以上的设备
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式