5.Android之image控件学习
Android中经常用到图片,比如图片浏览、图标等等,今天学习下image控件,image控件主要有ImageButton和ImageView两种。
(1)ImageButton
在布局文件增加:
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="21dp"
android:src="@drawable/ic_launcher" /> 效果:
如果想改变按钮图案,可以在drawable下放自定义图标,然后修改imagebutton里面src代码即可,如图



(2)ImageView
这个稍微复杂点,但也不是很难,我们先随便添加几张图片如图:
简单修改下xml文件,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.imagebutton.MainActivity" > <LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:src="@drawable/a" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="29dp" >
</RadioGroup> </LinearLayout> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/layout1"
android:layout_below="@+id/layout1"
android:text="上一个" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="下一个" /> </RelativeLayout>
接下来实现点击上一个或者下一个按钮,图片会发生改变功能,代码如下:
package com.example.imagebutton; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private Button prebutton = null;
private Button nextbutton = null;
private ImageView imageview1 = null;
public int i = 0;
private int[ ] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); prebutton = (Button)findViewById(R.id.button1);
nextbutton = (Button)findViewById(R.id.button2);
imageview1 = (ImageView)findViewById(R.id.imageView1);
prebutton.setOnClickListener(new prebuttonclick());
nextbutton.setOnClickListener(new nextbuttonclick()); imageview1.setImageResource(i);
} public class prebuttonclick implements OnClickListener { @Override
public void onClick(View v) {
if (i > 0)
{
imageview1.setImageResource(iImages[--i]);
}
else if (i <= 0)
{
i = 5;
imageview1.setImageResource(iImages[i]);
}
}
} public class nextbuttonclick implements OnClickListener {
@Override
public void onClick(View v) {
if (i < 5)
{
imageview1.setImageResource(iImages[++i]);
}
else if (i >= 5)
{
i = 0;
imageview1.setImageResource(iImages[i]);
} }
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
5.Android之image控件学习的更多相关文章
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...
- Android Material Design控件学习(一)——TabLayout的用法
前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...
- Android Material Design控件学习(二)——NavigationView的学习和使用
前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- 【ALearning】第三章 Android基本常见控件
本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...
随机推荐
- putty 访问 vmware中ubuntu 方法
putty访问虚拟机 从宿主机中用putty连接虚拟机中的Ubuntu. putty默认使用ssh方式连接,这需要在Ubuntu Server中安装ssh服务.使用命令sudo apt-get ...
- vue2.0 transition -- demo实践填坑
前言 vue1.0版本和2.0版本的过渡系统改变还是蛮彻底的,具体请自行详看文档介绍:https://vuefe.cn/v2/guide/migration.html#过渡.在使用2.0版本做过渡效果 ...
- 25Mybatis_查询缓存的基本知识
mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级和二级缓存的示意图:
- 动态调用webservice,不需要添加Web References
using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; ...
- 多线程BackgroundWorker
链接:http://www.cnblogs.com/yiyisawa/archive/2008/11/24/1339826.html 周六闲来无事,学习了多线程BackgroundWorker,以此记 ...
- 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)
1. 使用rails new 命令创建完的应用在自动执行bundle install不会成功,根据出错提示,判断原因有可能是被墙与https的证书的安全性问题. 作为开发环境,选用绕开的办法,在目录 ...
- [转]World Wind学习总结一
WW的纹理,DEM数据,及LOD模型 以earth为例 1. 地形数据: 默认浏览器纹理数据存放在/Cache/Earth/Images/NASA Landsat Imagery/NLT Landsa ...
- 动态下载苹果系统提供的多种中文字体(适合对字体有较多需求的应用比如阅读类的app)
链接:https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewControl ...
- 一个基于.NET平台的自动化/压力测试系统设计简述
AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...
- 嵌入式Linux利用Wifi搭建无线服务器(物联网实践之无线网关)
在 http://www.cnblogs.com/heat-man/p/4564539.html中,在嵌入式Linux开发板上我们从最底层实现了一个智能家居的远程控制系统,然而采取的是用网线连接到交换 ...