繁体   English   中英

动作栏图标定位Android

[英]Action Bar Icon Positioning Android

我已经在Toolbar类的帮助下为Android App创建了自定义操作栏。 它仍然在操作栏的左侧显示菜单按钮,单击后将转换为箭头。 我需要保留此图标,因为我具有侧面菜单。

我的问题是我在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与外部Layout权重一起导致了此问题。 因此,最后我删除了外部LinearLayout和Layout权重,将图标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"
        />

暂无
暂无

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

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