您的位置:首页 > 其它

《疯狂安卓讲义》P64 -- 实例:简单图片浏览器

2018-03-12 15:39 190 查看

<?xml version="1.0" encoding="utf-8"?><!--定义一个线性布局容器--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"></LinearLayout>

public class MainActivity extends AppCompatActivity {
    //定义一个访问图片的数组    int[] images = new int[] {            R.drawable.java,            R.drawable.javaee,            R.drawable.swift,            R.drawable.ajax,            R.drawable.html, };    int currentImg = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取LinearLayout布局容器        LinearLayout main = (LinearLayout) findViewById(R.id.root);        //程序创建ImageView组件        final ImageView image = new ImageView(this);        //将ImageView组件添加到LinearLayout布局容器中        main.addView(image);        //初始化时显示第一张图片        image.setImageResource(images[0]);        image.setOnClickListener(new View.OnClickListener()        {            @Override            public void onClick(View v)            {                //改变ImageView里显示的图片                image.setImageResource(images[++currentImg % images.length]);            }        });    }}<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.zxy.ex2">
    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="21"/>

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">

    <activity        android:name=".MainActivity"        android:label="@string/app_name">        android:theme="@style/AppTheme.NoActionBar">        <intent-filter>            <action android:name="android.intent.action.MAIN"/>            <category android:name="android.intent.category.LAUNCHER" />        </intent-filter>    </activity>
    </application></manifest>





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