您的位置:首页 > 移动开发 > Android开发

安卓案例-简单图片查看器

2016-11-30 17:28 218 查看
使用XML 布局文件和 java代码混合控制UI界面制作一个简单的图片查看器

app 演示图



布局文件

我们先在布局文件中定义一个简单的线性布局容器,该布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pub.weber.bym.shownextimage.MainActivity">

</LinearLayout>


java 代码

接下来在程序中获取该线性布局容器,并往该容器中添加组件。代码如下

public class MainActivity extends AppCompatActivity {
// 定义一个访问图片的数组
int images [] = new int[]{
R.drawable.a1,
R.drawable.a2,
R.drawable.a3,
R.drawable.a4,
R.drawable.a5,
R.drawable.a6
};
int currentImg = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取LinearLayout 布局容器
LinearLayout main = (LinearLayout) findViewById(R.id.activity_main);
final ImageView imageView = new ImageView(this);
main.addView(imageView);
// 初始化显示第一张图片
imageView.setImageResource(images[0]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(images[++currentImg % images.length]);
}
});
}
}


by web开发者 更多相关内容请访问: http://weber.pub/

本文地址: http://weber.pub/安卓笔记14(案例)-简单图片查看器/315.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片