5号总结
今天进行了androidstudio的学习,按照b站视频学习了2个小时左右。
今天按照视频指导,制作了相册app,比较low,只是图片和按钮btn的应用。
已经导出apk安装到了手机。
导出时遇到了 error: failed to read PNG signature: file does not start with PNG signature.
解决方案:图片在画图中打开然后保存为.pnj格式。
下面是代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_show"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@android:color/background_dark" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="图片信息" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout>
</LinearLayout>
mainactivity.java
package com.example.layout02;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mImage;
private TextView mText;
private int num;
private int index;
private int[] images;
private String[] title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
initData();
}
private void initData() {
title = new String[]{"第1张图片","第2张图片","第3张图片","第4张图片","第5张图片" };
images = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
mImage.setImageResource(images[0]);
mText.setText(title[0]);
num = title.length;
index = 0;
}
private void initView(){
mImage = findViewById(R.id.iv_show);
mText = findViewById(R.id.tv_show);
findViewById(R.id.btn_previous).setOnClickListener(this);
findViewById(R.id.btn_next).setOnClickListener(this);
}
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.btn_previous:
if(index==0) {
index = num - 1;
}else{
index--;
}
break;
case R.id.btn_next:
if(index==4)
{
index=0;}else{
index++;
}
break;
}
updateImageAndTitle();
}
private void updateImageAndTitle() {
mImage.setImageResource(images[index]);
mText.setText(title[index]);
}
}




浙公网安备 33010602011771号