我问过类似的问题here ... 我在答案中得到了一些教程。 但这个问题是不同的。因为这些方法都不适用于我的项目。
我想让工具栏中的所有图标居中
我测试了所有可用的方法...但图标始终 float 在左侧。
我想要居中图标(我更喜欢左图标向左浮动,右图标向右浮动,其他图标向中心 float )
Activity.Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" // I add vertical
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent" // I add match_parent
android:layout_gravity="center" /> // I add center
</LinearLayout>.
也在 tool_bar.xml 中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
android:layout_gravity="center" // I add center
xmlns:android="http://schemas.android.com/apk/res/android" />
在main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_forward"
android:title="forward"
android:icon="@drawable/ic_action_arrow_forward"
app:showAsAction="always"
></item>
<item
android:id="@+id/action_zoom_in"
android:title="zoom in"
android:icon="@drawable/ic_action_zoom_in"
app:showAsAction="always"
></item>
<item ...
以及 <item>
中的上述代码 (main_menu.xml)我尝试了所有这些代码:
android:layout_width="match_parent"
android:layout_weight=".2" // 20%
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
我真的不知道如何对齐图标。 我用谷歌搜索了几个小时,我尝试了所有 senario
我哪里错了? 对于所有测试,我看不到任何变化......
之前我想要 pic2(在上图中)但现在我只想居中!
对于我的所有测试,我只得到图片 1。没有任何变化。
最佳答案
修复问题,享受:)
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:background="?attr/colorPrimary">
<!-- Code for your Left Button -->
<ImageView
android:id="@+id/yourId"
android:src="@mipmap/yourLeftIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_gravity="left" />
<!-- Here is the main code for your Middle Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:id="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:id="@+id/button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:id="@+id/button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:id="@+id/button4" />
</LinearLayout>
<!-- Code for your Right Button -->
<ImageView
android:id="@+id/yourId"
android:src="@mipmap/yourRightIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
关于android - 如何在android中将工具栏中的图标居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31154029/