簡體   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