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文件,

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.imagebutton.MainActivity" >
10
11     <LinearLayout
12         android:id="@+id/layout1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:orientation="vertical">
16     <TextView
17         android:id="@+id/textView1"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="@string/hello_world" />
21
22     <ImageView
23         android:id="@+id/imageView1"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:layout_alignLeft="@+id/textView1"
27         android:layout_below="@+id/textView1"
28         android:layout_marginTop="18dp"
29         android:src="@drawable/a" />
30
31     <RadioGroup
32         android:id="@+id/radioGroup1"
33         android:layout_width="wrap_content"
34         android:layout_height="wrap_content"
35         android:layout_alignLeft="@+id/spinner1"
36         android:layout_below="@+id/spinner1"
37         android:layout_marginTop="29dp" >
38     </RadioGroup>
39
40     </LinearLayout>
41
42     <Button
43         android:id="@+id/button1"
44         android:layout_width="wrap_content"
45         android:layout_height="wrap_content"
46         android:layout_alignLeft="@+id/layout1"
47         android:layout_below="@+id/layout1"
48         android:text="上一个" />
49
50     <Button
51         android:id="@+id/button2"
52         android:layout_width="wrap_content"
53         android:layout_height="wrap_content"
54         android:layout_alignBottom="@+id/button1"
55         android:layout_centerHorizontal="true"
56         android:text="下一个" />
57
58 </RelativeLayout>

接下来实现点击上一个或者下一个按钮,图片会发生改变功能,代码如下:

 1 package com.example.imagebutton;
 2
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.ImageView;
11
12 public class MainActivity extends Activity {
13
14     private Button prebutton = null;
15     private Button nextbutton = null;
16     private ImageView imageview1 = null;
17     public int i = 0;
18     private int[ ] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
19
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24
25         prebutton = (Button)findViewById(R.id.button1);
26         nextbutton = (Button)findViewById(R.id.button2);
27         imageview1 = (ImageView)findViewById(R.id.imageView1);
28         prebutton.setOnClickListener(new prebuttonclick());
29         nextbutton.setOnClickListener(new nextbuttonclick());
30
31         imageview1.setImageResource(i);
32     }
33
34
35     public class prebuttonclick implements OnClickListener {
36
37         @Override
38         public void onClick(View v) {
39             if (i > 0)
40             {
41                 imageview1.setImageResource(iImages[--i]);
42             }
43             else if (i <= 0)
44             {
45                 i = 5;
46                 imageview1.setImageResource(iImages[i]);
47             }
48         }
49     }
50
51
52     public class nextbuttonclick implements OnClickListener {
53         @Override
54         public void onClick(View v) {
55             if (i < 5)
56             {
57                 imageview1.setImageResource(iImages[++i]);
58             }
59             else if (i >= 5)
60             {
61                 i = 0;
62                 imageview1.setImageResource(iImages[i]);
63             }
64
65         }
66     }
67
68     @Override
69     public boolean onCreateOptionsMenu(Menu menu) {
70         // Inflate the menu; this adds items to the action bar if it is present.
71         getMenuInflater().inflate(R.menu.main, menu);
72         return true;
73     }
74
75     @Override
76     public boolean onOptionsItemSelected(MenuItem item) {
77         // Handle action bar item clicks here. The action bar will
78         // automatically handle clicks on the Home/Up button, so long
79         // as you specify a parent activity in AndroidManifest.xml.
80         int id = item.getItemId();
81         if (id == R.id.action_settings) {
82             return true;
83         }
84         return super.onOptionsItemSelected(item);
85     }
86 }
时间: 2024-08-02 20:00:54

5.Android之image控件学习的相关文章

android学习五(android中基本控件的使用)

前面已经学了activity的一些使用,那么下面我们进行android中基本的控件的学习和使用. 1.android中的TextView控件 新建一个项目,项目名为UITest,才有默认的设置,修改布局文件的内容,如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" a

五、Android学习第四天补充——Android的常用控件(转)

(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的Android的几个常用控件的使用方法: 一.RadioGroup和RadioButton——单选按钮 二.Checkbox——复选框 三.Toast——提示框,会自动消失 四.ProgressBar——进度条工具 五.ListView——以列表形式将控件显示出来 下面就对这些内容做个详细的解释: 首

Android 中常见控件的介绍和使用

1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.lang.Object   ? android.view.View   ? android.widget.TextView 直接子类: Button, CheckedTextView, Chronometer, DigitalClock, EditText 间接子类: AutoCompleteTextV

Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

[转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46514791 CSDN 废墟的树] 上一篇博客我们学习了Android Design Support Library库中的 是个简单的组件,不了解的童鞋可以参考之前的博客 Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用. 这篇博客我们继续学习Design库中的其他四个组件,分别是

Android自己定义控件皮肤

Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有一定变化,用户体验十分好. 这当中,就涉及到了Android自己定义控件属性的操作方法,下面操作以实现自己定义button皮肤为例. 1. 我们要自己定义将要实现的外观状态.能够是图片或者是自己定义的xml,这是我们直接自己定义不同状态的颜色xml,在values文件夹下新建colors.xml,代

android 自己定义控件属性(TypedArray以及attrs解释)

近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这个学习,讲一下流程吧,兴许一篇还有应用. 1.attrs文件编写 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="titleText" for

【ALearning】第三章 Android基本常见控件

本章主要介绍基本的平常较多使用的控件,包括TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的LinearLayout布局,可先不必深究,后续章节将会详细介绍. TextView TextView控件的基本属性,android:layout_width 布局宽度android:layout_height 布局高度.这两个属性参数是必须的. TextView 中android:layout_width与

Android Design Support控件之DrawerLayout简单使用

DrawerLayout能够让我们在项目中非常方便地实现側滑菜单效果.如今主流的应用如QQ等都 採用的这样的效果. 这两天也是在学习Android Design Support的相关知识.网上有关这方面的文章介绍非常多.可是为了方便以后使用,还是把学习的知识做个简单记录.这次的代码也是在上一篇博客Android Design Support控件介绍之TabLayout的基础上加入的布局和代码. 主界面布局: <?xml version="1.0" encoding="u

Android自己定义控件:进度条的四种实现方式

前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://download.csdn.net/detail/chaoyu168/9616035 近期一直在学习自己定义控件,搜了很多大牛们Blog里分享的小教程.也上GitHub找了一些类似的控件进行学习.发现读起来都不太好懂,就想写这么一篇东西作为学习笔记吧. 一.控件介绍: 进度条在App中非经常见,比例如以下载