【问题标题】:How to set the image in button in android如何在android中的按钮中设置图像
【发布时间】:2015-09-30 12:02:29
【问题描述】:

我在 android 工作时总是遇到这个问题,那就是在按钮中设置图像。我有一个我想在按钮中设置的图像,所以我有这个按钮图像的多种分辨率,用于不同的存储桶,以支持多种屏幕尺寸。

以下是我的图片,分辨率为 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,而是使用 Button 的方式。那么任何人都可以告诉我在 button 或 Imagebutton 中使用图像的最佳方式是什么。

请告诉我为什么在我尝试在按钮中设置图像时总是发生这种情况,而最终当我使用图像视图时,我总是得到我想要的结果。

(如何使用 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>

所以任何人都请让我明白我做错了什么或者使用自定义图像的最佳方式是什么。 ??

【问题讨论】:

  • 你可以使用ImageButton
  • 我在我的问题中提到我想使用 Button ,而使用 imageButton 也需要图像的透明部分,看起来并不酷
  • “也将图像的透明部分放在里面”,你能澄清一下吗?
  • 我的意思是图像有角,并且默认情况下按钮似乎有填充,这是我的想法,有些东西不能让图像看起来像它应该的那样,我可以猜到最好的是,默认情况下我认为按钮是矩形的,所以必须有一些东西总是试图让按钮在纵横比上保持一致

标签: android android-layout android-button


【解决方案1】:

我测试了你的代码,没有问题...

由于您的图像宽度问题,这是由于不同设备上的屏幕分辨率,您可以将Linear Layout放在ScrollView

滚动视图之前的代码输出:

<ScrollView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|top"
    android:orientation="vertical"
    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"> 
   </LinearLayout>
</ScrollView>

滚动视图后的输出:

【讨论】:

  • 您是否要创建项目水平排列的菜单?
  • 尝试更改您的TextView android:layout_width
  • 是什么让纽扣变了?以及放置滚动视图后有什么帮助,为什么它在它之前不起作用
  • 因为它在当前分辨率下适合所有按钮,所以当分辨率高时它们适合,但当分辨率低时它会改变视图 widthheight 所以它们适合屏幕......当我们提供滚动视图它不会更改视图参数,而是将它们扩展到视图(超出屏幕高度)...根据定义:用户可以滚动的视图层次结构的布局容器,允许它大于物理显示link
  • 知道了,虽然没有必要使用滚动条,因为这些按钮必须适合顶部屏幕的一半
【解决方案2】:

你可以这样做:

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

在这种方法中,您还可以使用ImageView 代替ImageButton

【讨论】:

    【解决方案3】:

    使用android:drawableTop 而不是android:src

    【讨论】:

      【解决方案4】:

      试试这个,

      你可以使用

              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:drawableLeft="@drawable/ic_launcher"//按钮左侧的图像 android:drawableTop="@drawable/ic_launcher"//按钮顶部的图像 android:drawableBottom= "@drawable/ic_launcher"//按钮底部的图像 android:drawableRight="@drawable/ic_launcher"//rig 的图像
      • 您可以使用按钮的 android:drawableTop,Left,Right 等属性将图像设置在按钮的特定位置,如上、左、右。 ic_launcher 是我在按钮上设置的图像。
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:
        <?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">
        
        
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@mipmap/ic_launcher"/>
        
        </LinearLayout>
        

        你也可以使用

        android:drawableBottom ="@mipmap/ic_launcher"
        android:drawableTop="@mipmap/ic_launcher"
         android:drawableRight="@mipmap/ic_launcher"
        

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

         <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" />
        

        【讨论】:

        • 使用 ImageView 代替按钮
        • 是的,我上面已经提到了
        • 您已将图像放在相应的文件夹中,如 hdpi、mdpi... 或使用 common ?
        • 我已经具体相应地提出了
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多