适用于各种屏幕尺寸的Android ImageButton

时间:2015-02-01 11:58:09

标签: android imageview imagebutton

我正在尝试创建一个适合各种屏幕的图像按钮。但是,我无法使图像按钮正确适合更大的屏幕,而是在图像按钮上显示更小的图像。

到目前为止我尝试过:

  • 我已使用此link生成九个补丁图像,但即使我将文件添加到drawable:ldpi,mdpi,ldpi,xhdpi和xxhdpi文件夹,图像仍然比屏幕宽度短
  • 我尝试过使用android:scaleType="fitXY"android:adjustViewBounds="true"。这会根据每个屏幕尺寸进行调整,但您可以看到图像周边灰色图像按钮可点击区域的边缘。

下面的布局文件有什么问题,或者是否需要某些图像尺寸?

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

<ScrollView
    android:id="@+id/homepageScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <RelativeLayout
        android:id="@+id/homepageRelLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="fill" >

    <ImageButton
        android:id="@+id/homepageImageButton"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="40dp"
        android:src="@drawable/someImage" />

    </RelativeLayout>

</ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如果使用ImageView +单击侦听器,则会丢失按钮输入/输出效果。 也许你可以尝试给你的ImageButton一个透明的背景:

android:background="?android:selectableItemBackground"

不要使用@null@android:color/transparent,它们也会失去按钮效果。

顺便说一句,android:selectableItemBackground不能用于像2.x这样的旧Android版本

相关问题