如何实现一个机器人:背景不拉伸? [英] How to implement an android:background that doesn't stretch?

查看:120
本文介绍了如何实现一个机器人:背景不拉伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个大螺纹描述如何吃了蛋糕,并把它太,即:使用图像获得按钮,而不是的ImageButton 的(不允许的setText(),调整大小,等等)。

I found this great thread describing how to "eat the cake and have it too", i.e. use image for a Button instead of ImageButton (which doesn't allow SetText(), resizing, etc.).

这是通过查看属性来实现的:

This is achieved by using the View attribute:

android:background="@drawable/bgimage"

这个唯一的问题是,它拉伸图像以适合按钮的大小。

The only problem with this is that it stretches the image to fit the button size.

短硬编码一个固定的按钮大小(像素!),有没有办法告诉机器人的不可以伸展背景图像的一切,无论是产量还是垫吗?

Short of hard-coding a fixed button size (in pixels!), is there a way to tell Android not to stretch the background image at all and either crop or pad it?

推荐答案

您应该使用<一个href="http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable%28android.graphics.drawable.Drawable%29">ImageView如果你不希望它舒展。 背景图片总是会延伸到适合的视图。 您需要将其设置为可绘制的图像纵横强制的对象。

You should use ImageView if you don't want it to stretch. Background images will always stretch to fit the view. You need to set it as a Drawable to force the image aspect to the object.

另外,如果你采用按钮的想法,那么你就需要从拉伸力的比例在按钮prevent的形象。

Otherwise, if you are sticking with the Button idea, then you will need to force the scaling in the button to prevent the image from stretching.

如您

OnCreate(Bundle bundle) {
  //set content layout, etc up here

  //now adjust button sizes
  Button B = (Button) findViewById(R.id.somebutton);
  int someDimension = 50; //50pixels
  B.setWidth(someDimension);
  B.setHeight(someDimension);
}

这篇关于如何实现一个机器人:背景不拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆