简体   繁体   English

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

[英]Android ImageButton for various screen sizes

I am trying to create an image button which will cater for a variety of screens. 我正在尝试创建一个图像按钮,以适应各种屏幕。 However, I cannot get the image button to correctly fit a larger screen and instead shows a much smaller image on the image button. 但是,我无法使图像按钮正确地适合较大的屏幕,而是在图像按钮上显示了小得多的图像。

What I have tried so far: 到目前为止我尝试过的是:

  • I have used this link to generate nine-patch images, however even if i add the files to the drawable: ldpi, mdpi, ldpi, xhdpi and xxhdpi folders, the image is still shorter than the screen width 我已经使用此链接生成了九个补丁图像,但是即使我将文件添加到可绘制对象中:ldpi,mdpi,ldpi,xhdpi和xxhdpi文件夹,该图像仍然比屏幕宽度短
  • I have tried using android:scaleType="fitXY" and android:adjustViewBounds="true" . 我尝试使用android:scaleType="fitXY"android:adjustViewBounds="true" This does adjust to each screen size but you can see the edges of the grey Image Button clickable area around the outskirts of the image. 这确实会适应每个屏幕尺寸,但是您可以看到图像外围的灰色“图像按钮”可点击区域的边缘。

Is there anything wrong with my layout file below or is there certain image dimensions which are required? 我下面的布局文件有问题吗?还是需要某些图像尺寸?

<?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>

If you use ImageView + click listener, you will lose the button in/out effect. 如果使用ImageView +单击侦听器,则将失去按钮的进/出效果。 Maybe you can try to give your ImageButton a transparent background like this: 也许您可以尝试为ImageButton提供透明背景,如下所示:

android:background="?android:selectableItemBackground"

Don't use @null or @android:color/transparent , they will lose the button effect as well. 不要使用@null@android:color/transparent ,它们也会失去按钮效果。

Btw, android:selectableItemBackground doesn't work for old android version like 2.x 顺便说一句,android:selectableItemBackground不适用于旧的Android版本,如2.x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM