如何在 Android 按钮中更改图标大小

5
我有一个既有文本又有图像的按钮。该图像的大小与实际图像大小相同。如何更改按钮中图像的大小?
<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:drawableTop="@drawable/home_icon"
    android:text="Settings"
    />

尝试这个,android:layout_height="wrap_content" - User_1191
2
如果您正在使用图像作为可绘制对象,则需要使用其他工具而不是 Android 更改图像大小。 - Aditya Vyas-Lakhan
我需要一种方法,可以像10dip、10dip这样指定宽度和高度。 - Devesh Agrawal
5个回答

4

2

使用特定的测量方法来确定尺寸。例如:

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:background="@drawable/home_icon"
    android:text="Settings"
/>

如果您需要更多控制按钮的设计,则可以使用容器,如RelativeLayout来容纳图像和文本。然后将一个onClickListner分配给容器来将其转换为可点击的按钮。以下是一个示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/snazzy_button_with_image_above_text"
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:layout_gravity="center"
    android:layout_marginLeft="1dp"
    android:background="@color/SkyBlue"
    android:paddingBottom="2dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp" >

<ImageView
    android:id="@+id/your_image"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/white"
    android:contentDescription="image inside button"
    android:scaleType="centerCrop" />

<TextView
    android:id="@+id/your_text"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/your_image"
    android:layout_alignParentLeft="true"
    android:layout_gravity="center"
    android:layout_marginBottom="0dp"
    android:layout_marginTop="0dp"
    android:background="@color/translucent_black"
    android:gravity="bottom|center_horizontal"
    android:paddingTop="0dp"
    android:text="Your Button Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/white"
    android:textSize="10sp" />

</RelativeLayout>

在代码中将 onClickListener 分配为如下所示:
yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View clickedButton) {
    //button click action here 
}

我认为这会设置完整按钮的大小,而不仅仅是按钮上的图像。 - Devesh Agrawal
@DeveshAgrawal:我现在明白你想要将图像包含在按钮内。Android的一个美妙之处就是能够将任何布局转换为可点击的按钮。在你的情况下,你可以将图像和文本包装在LinearLayout中(如果你想更加花哨,也可以使用RelativeLayout)。现在,你可以完全控制图像和文本的大小和位置。在你的代码中,为包含的LinearLayout添加一个onClickListner。Voila,你将拥有一个完全符合你需求的按钮。 - PeteH
@DeveshAgrawal:我在我的答案中添加了一个使用RelativeLayout的示例。一旦你尝试过它,随时可以选择它作为批准的答案。 - PeteH

2

您应该使用ImageButton,并在android:src中指定图像,同时将android:scaletype设置为fitXY。


在您的代码中设置Drawable

Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10), 
                     (int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example

1

设置android:background="@drawable/image_name"

并指定宽度和高度,例如

android:layout_height="10dp"
android:layout_height="10dp"

如果您需要更多的图像渲染选项,可以尝试使用ImageButton而不是Button。 您可以在此处参考


0

简单的方法:在dimens.xml中添加下一行代码。这个参数可以改变AlertDialog中所有按钮图标的大小。

<resources>
    <dimen name="abc_alert_dialog_button_dimen">24dp</dimen>
</resources>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接