ActionBar中未缩放Android ImageView

问题描述 投票:0回答:1

作为学习Android的一部分,我正在开发带有底部导航栏的应用。导航选项卡之一具有带开关的操作栏,切换按钮和ImageView。我正在尝试缩放图像(绿色圆圈)以适合特定尺寸。它在设计视图中正确显示:

enter image description here

但是不能在仿真器中缩放:

enter image description here

这些是操作栏的xml文件:

top_nav_menu_acars:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/switch_connection_acars_menu"
        android:title=""
        app:actionLayout="@layout/switch_connection_acars_layout"
        app:showAsAction="always" />
</menu>

switch_connection_acars_layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch_connection_acars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="false"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/lamp_connection_acars"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentStart="false"
        android:layout_centerInParent="false"
        android:layout_gravity="center"
        android:layout_marginEnd="20dp"
        android:adjustViewBounds="true"
        android:background="@color/material_on_background_disabled"
        android:contentDescription="@string/lamp_connection_acars"
        android:cropToPadding="true"
        android:scaleType="fitXY"
        android:src="@drawable/connection_lamp_green"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/switch_connection_acars"
        app:layout_constraintTop_toTopOf="parent" />

    <ToggleButton
        android:id="@+id/switch_map_acars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:text="@string/switch_map_acars"
        android:textOff="@string/switch_map_acars_map"
        android:textOn="@string/switch_map_acars_sat"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/switch_connection_acars"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

有人知道为什么图像缩放不正确吗?谢谢!

android imageview scale
1个回答
0
投票

尝试一下,

<ImageView
        android:id="@+id/lamp_connection_acars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="false"
        android:layout_centerInParent="false"
        android:layout_gravity="center"
        android:layout_marginEnd="20dp"
        android:adjustViewBounds="true"
        android:background="@color/material_on_background_disabled"
        android:contentDescription="@string/lamp_connection_acars"
        android:cropToPadding="true"  
        android:maxWidth="40dp"  
        android:maxHeight="40dp"  
        android:scaleType="fitCenter"
        android:src="@drawable/connection_lamp_green"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/switch_connection_acars"
        app:layout_constraintTop_toTopOf="parent" />
© www.soinside.com 2019 - 2024. All rights reserved.