如何防止带有 3 个图像的底部工具栏被拉伸?

How can I prevent my bottom toolbar with 3 images from being stretched out?

我有一个包含三张图片的底部布局。我希望它们平均分配。为此,我使用了 layout_weight xml 属性。但是它们的视觉表现很糟糕——图像都被拉长了

图片尺寸为32*32。

此特定工具栏的布局代码是:

<?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="wrap_content"
android:id="@+id/toolbar_bottom"
xmlns:appo="http://schemas.android.com/apk/res-auto"
android:minHeight="50dp"
android:background="#ffff8c0e"
appo:theme="@style/ToolbarTheme"
android:minWidth="50dp"
android:elevation="10dp"
>
<LinearLayout
    android:id="@+id/toolbarmenucontainer"
    android:layout_width="match_parent"
    android:weightSum="3"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="0dp"
        android:id="@+id/camera_image"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"

        android:scaleType="fitXY"
        android:src="@drawable/camera_icon"
        />

    <ImageView
        android:id="@+id/news_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:clickable="true"
        android:scaleType="fitXY"
        android:layout_weight = "1"
        android:src="@drawable/new_icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <ImageView
        android:id="@+id/facebook_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:clickable="true"
        android:scaleType="fitXY"
        android:src="@drawable/facebooc_image"
       />
</LinearLayout>

如何让它们正确显示?

ImageView 包裹在另一个布局中,比如 RelativeLayout

即做这样的事情。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appo="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff8c0e"
    android:elevation="10dp"
    android:minHeight="50dp"
    android:minWidth="50dp"
    appo:theme="@style/ToolbarTheme" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbarmenucontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/camera_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:clickable="true"
                android:scaleType="fitXY"
                android:src="@drawable/camera_icon" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/news_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerInParent="true"
                android:clickable="true"
                android:scaleType="fitXY"
                android:src="@drawable/new_icon" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/facebook_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:clickable="true"
                android:scaleType="fitXY"
                android:src="@drawable/facebooc_image" />
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

希望这会有所帮助。

Remove android:scaleType="fitXY" from your code.Because All it does is scale the bitmap up to the size of the ImageView.

参考这个

<LinearLayout
        android:id="@+id/toolbarmenucontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3">

        <ImageView
            android:id="@+id/camera_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/news_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/facebook_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

像这样使用 RelativeLayout:

<RelativeLayout  android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/camera_image"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/camera_icon"
        android:scaleType="fitXY"
        />

    <ImageView
        android:id="@+id/news_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:src="@drawable/new_icon"
        android:scaleType="fitXY"/>
    <ImageView
        android:id="@+id/facebook_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:scaleType="fitXY"
        android:src="@drawable/facebooc_image"/></RelativeLayout>

根据需要设置图像视图比例类型,有不同的选项

android:scaleType="fitXY"

centercenterCropcenterInsidefitCenter

有这个很好的 library 解决了问题:)。它使用底部导航。