Android imagebutton 如何更改图标大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11538747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:57:49  来源:igfitidea点击:

Android imagebutton how to change icon size

androidiconsimagebutton

提问by Loic O.

I have been working on interfaces lately and I have many problems with imagebutton sizes. I'm very bad at designing stuff and I'm having major problems with the Android Icons. Thus, I have some questions regarding those.

我最近一直在研究界面,我在图像按钮大小方面遇到了很多问题。我在设计东西方面非常糟糕,而且我在 Android 图标方面遇到了重大问题。因此,我对这些有一些疑问。

First of all this is my current layout:

首先这是我目前的布局:

enter image description here

在此处输入图片说明

As you can see, I have two icons that have different sizes and that's not good. I can't seem to force them to take the same size.

如您所见,我有两个大小不同的图标,这并不好。我似乎无法强迫它们采用相同的尺寸。

My questions are:

我的问题是:

1 - How do I force them to take the same size?

1 - 我如何强制它们采用相同的尺寸?

2 - What are the recommended size differences for hdpi, ldpi,mdpi and xhdpi?

2 - hdpi、ldpi、mdpi 和 xhdpi 的推荐大小差异是多少?

3 - Why, if I set the imageButton background color to the same color as the layout background, the Icon is moved as if there was no button in the background?

3 - 为什么,如果我将 imageButton 背景颜色设置为与布局背景相同的颜色,图标会像背景中没有按钮一样移动?

Example: enter image description here

例子: 在此处输入图片说明

Thanks in advance!!

提前致谢!!

回答by pixelscreen

For these ImageButtons set the images using (instead of src):

对于这些 ImageButtons 使用(而不是 src)设置图像:

android:background="@drawable/image_name"

Next thing is set:

接下来设置:

android:layout_height="10dp" //or any size required
android:layout_width="10dp" //or any size required ... let these 2 sizes be same for both the ImageButtons

"dp" or "dip" is device independent pixels. The android system will take care of the resizing for different screen resolutions

“dp”或“dip”是与设备无关的像素。android 系统将负责调整不同屏幕分辨率的大小

Also, a better thing to do would be to set-up hover images for your button separately.

此外,更好的做法是分别为您的按钮设置悬停图像。

回答by bmavus

type this:

输入:

in java:

在 Java 中:

YOUR_VIEW.setLayoutParams(new LayoutParams(width,height));

or in XML:

或在 XML 中:

android:layout_height="10px"
android:layout_width="10px"

回答by shassss

android:layout_height="10dp"

android:layout_width="10dp"

Dont keep in px, always you have to keep in only dp

不要保持px,总是你只需要保持dp

回答by ScouseChris

As the other answers have said, set the button sizes using dp or dip in the layout xml files.

正如其他答案所说,在布局 xml 文件中使用 dp 或 dip 设置按钮大小。

For handling different screen sizes see this doc: http://developer.android.com/guide/practices/screens_support.html

要处理不同的屏幕尺寸,请参阅此文档:http: //developer.android.com/guide/practices/screens_support.html