适用于Android中的ImageButton中的图像

RDX*_*RDX 134 android image scale imagebutton android-linearlayout

我的活动中有6个ImageButton,我通过我的代码设置图像(不使用xml).

我希望它们覆盖75%的按钮区域.但是,由于某些图像覆盖的区域较少,因此有些图像太大而无法适应图像按钮.如何以编程方式调整大小并显示它们?以下是截图

在此输入图像描述 下面是xml文件

<?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="vertical"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     >
   <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ImageButton          

            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_topleft"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"
            />
        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_topright"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_repeat"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     
             />

              <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_next"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     
             />

    </LinearLayout>    
   <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_bottomleft"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"                                 
             />
        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_bottomright"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"                  
            />        
    </LinearLayout>        

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以及myClass.java的片段:

public void addImageButtons()
    {
        iB_topleft = (ImageButton) findViewById(R.id.button_topleft);
        iB_topright = (ImageButton) findViewById(R.id.button_topright);
        iB_bottomleft = (ImageButton) findViewById(R.id.button_bottomleft);
        iB_bottomright = (ImageButton) findViewById(R.id.button_bottomright);
        iB_next = (ImageButton) findViewById(R.id.button_next);
        iB_repeat = (ImageButton) findViewById(R.id.button_repeat);
    }

    public void setImageNextAndRepeat()
    {

    iB_topleft .setImageResource(R.drawable.aa);
        iB_topright.setImageResource(R.drawable.bb);   

    iB_bottomleft.setImageResource(R.drawable.cc);
        iB_bottomright.setImageResource(R.drawable.dd);   

        iB_next.setImageResource(R.drawable.next);
        iB_repeat.setImageResource(R.drawable.repeat);      
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*yle 356

我希望它们覆盖75%的按钮区域.

使用android:padding="20dp"(根据需要调整填充)来控制图像占据按钮的程度.

但是在某些图像覆盖较少区域的情况下,有些图像太大而无法适应图像按钮.如何以编程方式调整大小并显示它们?

使用a android:scaleType="fitCenter"来让Android缩放图像,并android:adjustViewBounds="true"让它们通过缩放来调整边界.

所有这些属性都可以ImageButton在运行时的每个代码中设置.但是,在我看来,在xml中设置和预览要容易得多.

此外,不要使用sp除文本大小以外的任何内容,它会根据用户设置的文本大小首选项进行缩放,因此sp如果用户具有"大"文本设置,则您的尺寸将大于预期尺寸.请dp改用,因为它没有按用户的文字大小首选项进行缩放.

这是每个按钮应该是什么样子的片段:

    <ImageButton
        android:id="@+id/button_topleft"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:padding="20dp"
        android:scaleType="fitCenter" />
Run Code Online (Sandbox Code Playgroud)

样本按钮

  • 对于那些看起来,`ImageButton`方法`setScaleType(ImageView.ScaleType.CENTER)``setAdjustViewBounds(true)`以编程方式执行此操作. (6认同)
  • 有效!保存了我的小时搜索!爱你 (2认同)

Pra*_*ode 28

我正在使用以下代码 xml

android:adjustViewBounds="true"
android:scaleType="centerInside"
Run Code Online (Sandbox Code Playgroud)

  • 我有一个相当“扁平”的矩形,这对我来说效果很好......不过有一个问题:确保你使用“android:src”而不是“android:background”。+1。 (2认同)

Swa*_*oid 8

尝试android:scaleType="fitXY"在i-Imagebutton xml中使用


Ser*_*m's 6

android:scaleType="fitCenter"满意地使用.


Nha*_*inh 6

请参阅以下链接并尝试找到您真正想要的:

ImageView.ScaleType CENTER 将图像在视图中居中,但不执行缩放。

ImageView.ScaleType CENTER_CROP 均匀缩放图像(保持图像的纵横比),以便图像的两个尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)。

ImageView.ScaleType CENTER_INSIDE 均匀缩放图像(保持图像的纵横比),以便图像的两个尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)。

ImageView.ScaleType FIT_CENTER 使用 CENTER 缩放图像。

ImageView.ScaleType FIT_END 使用 END 缩放图像。

ImageView.ScaleType FIT_START 使用 START 缩放图像。

ImageView.ScaleType FIT_XY 使用 FILL 缩放图像。

ImageView.ScaleType MATRIX 绘图时使用图像矩阵进行缩放。

https://developer.android.com/reference/android/widget/ImageView.ScaleType.html