如何在android中设置图像输入按钮

如何在android中设置图像输入按钮,android,android-layout,android-button,Android,Android Layout,Android Button,我在安卓系统中工作时总是遇到这个问题,那就是在按钮中设置图像。我有一个图像,我想设置在按钮,所以我有多个不同的桶按钮图像分辨率,以便支持多种屏幕大小 下面是我的图片,分辨率为85*85 现在,当我想在按钮中设置它时,我得到了图像螺钉。看起来图像的宽度比图像的高度要大,而实际上并不是这样。在按钮中设置图像后,让我与您分享图片。本屏幕是android三星galaxy s4设备的简称,该设备为xxhd设备 现在你可以很容易地看到它看起来有多糟糕 这就是我在xml中设置它的方式 <Button

我在安卓系统中工作时总是遇到这个问题,那就是在按钮中设置图像。我有一个图像,我想设置在按钮,所以我有多个不同的桶按钮图像分辨率,以便支持多种屏幕大小

下面是我的图片,分辨率为85*85

现在,当我想在按钮中设置它时,我得到了图像螺钉。看起来图像的宽度比图像的高度要大,而实际上并不是这样。在按钮中设置图像后,让我与您分享图片。本屏幕是android三星galaxy s4设备的简称,该设备为xxhd设备

现在你可以很容易地看到它看起来有多糟糕

这就是我在xml中设置它的方式

<Button
            android:id="@+id/btn_flip_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/flip_image"
            />

但是,当我在Imageview中设置此图像时,一切看起来都像预期的那样好。因此,问题不在于图像或xml,而在于按钮的使用方式。那么,有谁能告诉我使用ImageIn按钮或Imagebutton按钮的最佳方式吗

请告诉我,当我尝试在按钮中设置图像时,为什么总是出现这种情况,而在最后使用imageview时,我总是能得到我想要的结果

(如何使用Imageview使我失去了 默认情况下,按钮已自动关闭。)

**Edit1**这是我使用按钮的xml部分

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|top"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        android:background="@drawable/menu_bg"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:gravity="center"
        >

        <Button
            android:id="@+id/btn_flip_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/flip_image"

            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="5dp"
            />

        <Button
            android:id="@+id/btn_flip_suit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/flip_suit" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="5dp"
            />

        <Button
            android:id="@+id/btn_change_suit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/change_suit" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="5dp"
            />

        <Button
            android:id="@+id/btn_share"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content"

            android:background="@drawable/share_btn" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="5dp"
           />

        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:background="@drawable/save_image"

            />
    </LinearLayout>

所以任何人都请让我了解我做错了什么,或者在自定义图像中使用按钮的最佳方式是什么

试试这个

你可以用

        android:drawableLeft="@drawable/ic_launcher"//image for left side in button
        android:drawableTop="@drawable/ic_launcher"//image for top side in button
        android:drawableBottom="@drawable/ic_launcher"//image for bottom side in button
        android:drawableRight="@drawable/ic_launcher"//image for right side in button
你的代码是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn_flip_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/flip_image" />

</RelativeLayout>


使用
android:drawableTop
而不是
android:src
我已经测试了你的代码,里面没有问题

由于图像宽度问题,这是由于不同设备上的屏幕分辨率,因此您可以将
线性布局
放入
滚动视图

scrollview之前的代码输出:


滚动查看后的输出:

您也可以尝试使用ImageView而不是按钮

 <ImageView
        android:id="@+id/btn_flip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/flip_image" />

我总是使用背景,因为它不会改变图像尺寸:

<Button
        android:id="@+id/btn_flip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/flip_image" />

您可以这样做:

 <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/your_img" />


在这种方法中,你也可以使用
ImageView
而不是
ImageButton

你可以使用ImageButton我在问题中提到的我想要使用按钮,而使用ImageButton也会获取图像内部的透明部分,这看起来并不酷“也会获取图像内部的透明部分”,你能澄清一下吗?我的意思是图像有角,默认情况下按钮有填充物,这是我的想法,有些东西不让图像看起来像它应该的样子,我能猜到最好的是,默认情况下我认为按钮是矩形的,所以一定有什么东西总是试图让按钮在外观上保持一致。什么是android:drawableLeft=“@drawable/ic_launcher”//image用于按钮的左侧android:drawableTop=“@drawable/ic_launcher”//image用于按钮的顶部android:drawableBottom=“@drawable/ic_launcher”//按钮底部的图像android:drawableRight=“@drawable/ic_launcher”//image for Right您可以使用按钮的android:drawableTop、Left、Right等属性,将图像设置在按钮的特定位置,如顶部、左侧、右侧。ic_launcher是我的图像,设置在按钮上。使用ImageView而不是buttonyeah,我在上面提到过,你已经将图像放在各自的文件夹中,如hdpi、mdpi。。。还是使用common?我已经详细说明了您是否试图创建水平排列的菜单项?尝试更改您的
TextView
android:layout\u width
是什么让按钮旋转?放置滚动视图后有什么帮助,为什么它之前不起作用,因为它在当前分辨率下适合所有按钮,所以当分辨率较高时,它们适合,但当分辨率较低时,它会更改视图
宽度
高度
,以便它们适合屏幕…当我们提供滚动视图时,它不会更改视图参数,相反,它将它们扩展到视图(超出屏幕高度)。。。根据定义:视图层次结构的布局容器,用户可以滚动,允许其大于实际显示,但无需使用滚动条,因为这些按钮必须从顶部适合屏幕的一半
 <ImageView
        android:id="@+id/btn_flip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/flip_image" />
<Button
        android:id="@+id/btn_flip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/flip_image" />
 <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/your_img" />