【问题标题】:ImageButton in Android xml (image is too small and horrible resolution)Android xml中的ImageButton(图像太小且分辨率可怕)
【发布时间】:2014-01-18 01:17:24
【问题描述】:

我有以下 3 个图像按钮。代码如下所示,三个按钮看起来相同。 问题:

  1. 图片没有完全填满按钮的大小
  2. 图像的分辨率很差。我将图像保存在所有 res/drawable 文件夹(drawable-hdpi、mdpi 等)中。图像由“Android 图标集”向导加载。

有什么想法吗?我一直在使用 padding 和 scaleType,因为我将它们视为其他帖子的解决方案。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

<ImageButton
    android:id="@+id/abutton"
    android:src="@drawable/a_button_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="5dp"
    android:scaleType="fitXY"/>

<ImageButton
    android:id="@+id/button"        
    android:src="@drawable/b_button_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:scaleType="fitXY"/>

 </LinearLayout>

 <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_marginTop="10dp"> 

<ImageButton
    android:id="@+id/cbutton"        
    android:src="@drawable/d_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="20dp"
    android:scaleType="fitXY"/>

<ImageButton
    android:id="@+id/dbutton"        
    android:src="@drawable/about_a_button"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="20dp"
    android:scaleType="fitXY"/>

    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 这可能有助于解决问题?图标没有占据整个按钮大小怎么办?
  • 但是您使用的图片尺寸是多少?因为我总是使用我想要的精确尺寸并且从不需要缩放:fitXY。

标签: android android-linearlayout imagebutton


【解决方案1】:

不要将drawable分配给android:src属性,而是将其分配给ImageButtonandroid:background属性。

您可以尝试设置android:adjustViewBounds="true",这将使ImageButton 调整其边界以保持其可绘制对象的纵横比

PS:如果您将图像设置为 ImageButton 的背景,则图像将缩放到 ImageButton 的任何大小。除此之外,src 是前景图像,background 是背景图像。

编辑:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <!-- The two linear layouts will have equal widths. -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="5dp" >

        <!-- The two image button will fill the linear layout along width and have height 150dp -->
        <ImageButton
            android:id="@+id/abutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />

        <ImageButton
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="5dp" >

        <ImageButton
            android:id="@+id/cbutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />

        <ImageButton
            android:id="@+id/dbutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />
    </LinearLayout>

</LinearLayout>

希望这会有所帮助。

【讨论】:

  • 更改为 android:background 只是删除图像按钮的背景(而不是变灰,它与背景匹配)。实际图像仍然是同一侧。 android:adjustViewBounds="true" 对我没有任何改变。还有其他想法吗?
【解决方案2】:

试试 通过删除填充并将背景设置为“空”。 在分辨率的情况下,我认为它正在拍摄低分辨率的图像。

<ImageButton
    android:id="@+id/dbutton"        
    android:src="@drawable/about_a_button"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:background="@null"
    android:scaleType="fitXY"/>

【讨论】:

  • 这不会改变任何东西。
【解决方案3】:

我遇到了同样的问题。没有其他人为我工作。最后这是一个简单的问题。默认情况下,ImageButton 周围存在填充。所以使用:

<ImageButton>
...
    android:padding="0dp"
...
</ImageButton>

它会解决问题。

【讨论】:

  • 太奇怪了,谢谢它解决了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
相关资源
最近更新 更多