Button 与ImageButton区别

ImageButton只能显示图片而不能显示文字,解决这个问题:设置Button的background属性可以有图片也可以有文字

  • android:text属性,在按钮上显示文字,ImageButton不能有文字
  • android:background 可以给按钮指定背景图片或颜色
  • android:src给ImageButton设置Drawable资源文件
  • android:adjustViewBounds=”true” 调整边框以适应图片大小

如何给Button添加鼠标点击事件如下:

Button sharmeBtn=(Button)findViewById(R.id.sharmeBtn);
sharmeBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.i("sharme",“welcome here! dear my Button Click”);
}
});

<!–  Limit to exactly 70×70, with 10 pixels of padding all around –>

<ImageView
android:src="@drawable/stat_happy"
android:background="#FFFFFFFF"
android:adjustViewBounds="true" //android:adjustViewBounds 如果设置为true 图像将自动调整自己的宽度
android:maxWidth="70dip" //android:maxWidth 设置图像的最大宽
android:maxHeight="70dip" //android:maxHeight 设置图像的最大高

android:scaleType="centerInside" //android:scaleType 控制如何调整图像大小或者移动范围,以配合ImageView 的大小
android:padding="10dip"  //android:paddingTop 与上节点边距的填充; android:padding 设置上、下、左、右的填充
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

实现ImageButton 图片自动适应控件大小:

1. <ImageButton android:id="@+id/imgbtn_pause" android:src="@drawable/pause"  
        android:background="#00000000" 
            android:visibility="gone" android:layout_width="wrap_content"  
            android:layout_height="wrap_content"></ImageButton>
2. <ImageButton android:id="@+id/imgbtn_next" android:background="@drawable/next" 
           android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>

android:scaleType  setScaleType(ImageView.ScaleType) //控制为了使图片适合 ImageView 的大小,应该如何变更图片大小或移动图片.

用于设置该视图支持的最大高度的可选参数。只有 setAdjustViewBounds(boolean) 为真时有效。 要设置图像最大尺寸为 100×100,并保持原始比率,做法如下: 1) 设置 adjustViewBounds 为真; 2) 设置 maxWidth 和 maxHeight 为 100; 3) 设置宽、高的布局参数为 WRAP_CONTENT。注意,如果原始图像较小,即使设置了该参数,图像仍然要比 100×100 小。如果要设置图片为 固定大小, 需要在布局参数中指定大小,并使用setScaleType(android.widget.ImageView.ScaleType) 函数来检测,如何将其调整到适当的大小。

一定是尺寸值,由浮点数后跟长度单位组成。比如“14.5sp”。 可用单位有:px (像素)dp (密度/设备无关像素)sp (基于首选字体大小的缩放像素)、 in (英寸), mm (毫米).

by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio.

However, make sure ImageView using android:src="..." rather than android:background="...".

  • src= makes it scale the image maintaining aspect ratio, but
  • background=makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)

android:adjustViewBounds=true

to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: