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

时间:2015-12-10 06:58:28

标签: android

我有一个同时包含文字和图片的按钮。它所采用的图像尺寸是图像的实际尺寸。如何更改按钮中图像的大小?

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

4 个答案:

答案 0 :(得分: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之类的容器来保存图像和文本。然后将onClickListener分配给容器,将其转换为可单击的按钮。这是一个例子:

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

答案 1 :(得分: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

答案 2 :(得分:1)

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

并给出宽度和高度,如

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

或者您可以尝试使用ImageButton而不是Button,因为ImageButton具有比Button更多的图像渲染选项 You can refer here

答案 3 :(得分:0)

设置

  

机器人:scaleType = “fitXY”

另外,wrap_content是宽度和高度的首选参数,因此操作系统将根据屏幕大小自动调整。 请参阅 Android - Image Button ScaleAdjusting the size of an ImageButton in Android