【问题标题】:ImageButton aspect ratio is not correctImageButton 纵横比不正确
【发布时间】:2018-12-22 23:17:08
【问题描述】:

我正在尝试将ImageButton 和 TextView 并排放置在 LinearLayout 中,按钮垂直大到足以适合布局 - 希望这应该适合宽度。布局的其余部分应由文本视图占据。

目前,我的 xml 文件中有以下布局:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".08"
    android:background="#004D79"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/helpButton"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:cropToPadding="false"
        android:layout_margin="3dp"
        android:adjustViewBounds="true"
        android:background="@drawable/help" /> 

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#000000"
        android:text="Some placeholder text"
        android:textColor="#ffffff"
        />

  </LinearLayout>

此布局占用了 8% 的垂直空间。按钮的图像为 500x500。 但是,我遇到了一些问题! 当我以“原样”上面的布局运行时,imagebutton 是正确的高度,但它占据了布局的整个宽度;根本没有文本视图 - 所以看起来android:adjustViewBounds="true" 不起作用。 但是,如果我用android:src="@drawable/help" 替换android:background="@drawable/help",我会在布局的左侧得到一个小正方形区域,但上面没有图像。文本视图位于“按钮”的右侧,但它不覆盖布局的其余部分;它被包裹起来,以便它足够大以包含占位符文本。

我必须在我的应用程序中以编程方式调整按钮和文本视图的大小,例如 OnMapReady(...)

编辑:我刚刚验证了图像文件在 res/drawable/help.png 中 - 但是为什么它应该出现在背景中(大小错误)但没有 src(大小正确) ) 对我来说是个谜。

【问题讨论】:

    标签: android android-linearlayout imagebutton


    【解决方案1】:

    android:adjustViewBounds需要和android:src一起使用,当android:adjustViewBounds为真时ImageButton会调整其边界以保持其drawable的纵横比,尺寸可能会变大。

    android:src,android:width,android:height 确定视图的大小,android:background 将被拉伸以填充整个视图,android:scaleType 控制如何调整图像的大小或移动以匹配这个图片按钮

    你想要的布局可能要设置android:srcandroid:scaleType属性

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal"
        android:gravity="center_vertical">
    
        <ImageButton
            android:id="@+id/helpButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_margin="3dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2016-03-11
      相关资源
      最近更新 更多