简体   繁体   English

动作栏图标定位Android

[英]Action Bar Icon Positioning Android

I have created a custom action bar for my Android App, with the help of the Toolbar class. 我已经在Toolbar类的帮助下为Android App创建了自定义操作栏。 It still shows the menu button to the left of the action bar, which converts to an arrow when click. 它仍然在操作栏的左侧显示菜单按钮,单击后将转换为箭头。 I need to keep this icon as I have a side menu. 我需要保留此图标,因为我具有侧面菜单。

The problem I have is that the other icons I have, which are defined in my XML file are not centering correctly. 我的问题是我在XML文件中定义的其他图标没有正确居中。

Below is an example of my aim, but it isn't a perfect example as there are 3 icons in the middle, not 2. 下面是我目标的一个示例,但这并不是一个完美的示例,因为中间有3个图标,而不是2个。

问题的例子

I am aware that the issue can be fixed quickly by just removing the android menu icon to the left, but I would like to keep it if possible. 我知道,只需删除左侧的android菜单图标即可快速解决此问题,但我想尽可能保留它。 I am also aware that my icons are centering, but are centering based on the area to the right of the menu icon. 我也知道我的图标居中,但是基于菜单图标右边的区域居中。

<?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>

Am I meant to be setting the position of these icons programatically and fixed positioned? 我是否打算以编程方式设置这些图标的位置并固定位置? Or am I totally missing something here? 还是我完全想念这里的东西?

Can someone point me in the right direction please. 有人可以指出我正确的方向。 Thank you! 谢谢!

I solved my problem! 我解决了我的问题!

It seems that the outer LinearLayout was causing the issue, along with the outer Layout weights. 似乎外部LinearLayout与外部Layout权重一起导致了此问题。 So in the end I removed the outer LinearLayout and Layout weights, updated icon 4 to have a width of wrap content and the height fill parent and it worked. 因此,最后我删除了外部LinearLayout和Layout权重,将图标4更新为具有环绕内容的宽度和高度填充父级,并且它起作用了。

For anyone interested please see the below code. 对于任何有兴趣的人,请参见下面的代码。

<?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"
        />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM