操作栏图标定位 Android
Action Bar Icon Positioning Android
我在工具栏 class 的帮助下为我的 Android 应用程序创建了自定义操作栏。它仍然在操作栏的左侧显示菜单按钮,单击时会转换为箭头。我需要保留此图标,因为我有侧边菜单。
我遇到的问题是我的 XML 文件中定义的其他图标没有正确居中。
下面是我的目标示例,但这不是一个完美的示例,因为中间有 3 个图标,而不是 2 个。
我知道只需删除左侧的 android 菜单图标即可快速解决此问题,但我希望尽可能保留它。我也知道我的图标是居中的,但是是基于菜单图标右侧的区域居中的。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize"
android:elevation="4dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:id="@+id/top_bar"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Icon1"
android:src="@drawable/icon1"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginRight="10dp"
android:alpha="0.2"/>
<ImageButton
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icon2"
android:src="@drawable/icon2"
android:background="@null"
android:tint="#ffffff"
android:maxHeight="25dp"
/>
<ImageButton
android:id="@+id/icon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon3"
android:src="@drawable/icon3"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginLeft="10dp"
android:alpha="0.2" />
</LinearLayout>
<ImageButton
android:id="@+id/icon4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="icon4"
android:src="@drawable/icon4'"
android:background="@null"
android:tint="#FFFFFF"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:layout_gravity="right|center_vertical"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
我是否打算以编程方式设置这些图标的位置并固定位置?还是我在这里完全遗漏了什么?
请有人指出我正确的方向。谢谢!
我的问题解决了!
问题似乎是由外部 LinearLayout 以及外部布局权重引起的。所以最后我删除了外部 LinearLayout 和布局权重,更新了图标 4 使其具有环绕内容的宽度和填充父级的高度并且它起作用了。
任何感兴趣的人请看下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/actionBarSize"
android:elevation="4dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon1"
android:src="@drawable/icon1"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginRight="10dp"
android:alpha="0.2"/>
<ImageButton
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icon2"
android:src="@drawable/icon2"
android:background="@null"
android:tint="#ffffff"
android:maxHeight="25dp"
/>
<ImageButton
android:id="@+id/icon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon3"
android:src="@drawable/icon3"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginLeft="10dp"
android:alpha="0.2" />
</LinearLayout>
<ImageButton
android:id="@+id/icon4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="icon4"
android:src="@drawable/icon4"
android:background="@null"
android:tint="#FFFFFF"
android:layout_marginTop="0dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_gravity="right|center_vertical"
/>
我在工具栏 class 的帮助下为我的 Android 应用程序创建了自定义操作栏。它仍然在操作栏的左侧显示菜单按钮,单击时会转换为箭头。我需要保留此图标,因为我有侧边菜单。
我遇到的问题是我的 XML 文件中定义的其他图标没有正确居中。
下面是我的目标示例,但这不是一个完美的示例,因为中间有 3 个图标,而不是 2 个。
我知道只需删除左侧的 android 菜单图标即可快速解决此问题,但我希望尽可能保留它。我也知道我的图标是居中的,但是是基于菜单图标右侧的区域居中的。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize"
android:elevation="4dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:id="@+id/top_bar"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Icon1"
android:src="@drawable/icon1"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginRight="10dp"
android:alpha="0.2"/>
<ImageButton
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icon2"
android:src="@drawable/icon2"
android:background="@null"
android:tint="#ffffff"
android:maxHeight="25dp"
/>
<ImageButton
android:id="@+id/icon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon3"
android:src="@drawable/icon3"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginLeft="10dp"
android:alpha="0.2" />
</LinearLayout>
<ImageButton
android:id="@+id/icon4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="icon4"
android:src="@drawable/icon4'"
android:background="@null"
android:tint="#FFFFFF"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:layout_gravity="right|center_vertical"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
我是否打算以编程方式设置这些图标的位置并固定位置?还是我在这里完全遗漏了什么?
请有人指出我正确的方向。谢谢!
我的问题解决了!
问题似乎是由外部 LinearLayout 以及外部布局权重引起的。所以最后我删除了外部 LinearLayout 和布局权重,更新了图标 4 使其具有环绕内容的宽度和填充父级的高度并且它起作用了。
任何感兴趣的人请看下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/actionBarSize"
android:elevation="4dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon1"
android:src="@drawable/icon1"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginRight="10dp"
android:alpha="0.2"/>
<ImageButton
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icon2"
android:src="@drawable/icon2"
android:background="@null"
android:tint="#ffffff"
android:maxHeight="25dp"
/>
<ImageButton
android:id="@+id/icon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon3"
android:src="@drawable/icon3"
android:background="@null"
android:tint="#ffffffff"
android:layout_marginLeft="10dp"
android:alpha="0.2" />
</LinearLayout>
<ImageButton
android:id="@+id/icon4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="icon4"
android:src="@drawable/icon4"
android:background="@null"
android:tint="#FFFFFF"
android:layout_marginTop="0dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_gravity="right|center_vertical"
/>