- Button
- EditText
- ImageView
- 布局
- ListView
- RecyclerView
Button EditText ImageView
在xml布局文件中创建button,活动主初始化对象,注册监听事件 ;这些都是较为简单的,xml中设置ID、text、height、width等等
布局
- 线性布局LinearLayout :将它所包含的控件从该线性方向依次排列(最常用的布局方式)
- 相对布局RelativeLayout: 通过相对定位的方式将控件摆放于布局的任意位置
等等
ListView
- ListView的必要性:手机屏幕有限,当有大量的数据需要展示的时候,就需要使用该控件;
- 功能:通过使用上下滑动的方式,将新的数据展现出来,旧的数据移出屏幕
- 最简单的用例: 创建一个ListView控件,活动中设置一串字符串,对ListView使用适配器,就能够轻松实现适配器的功能。
private String[] data = {"apple","banana","orange","watermelon","Pear","grape","apple","banana","orange","watermelon","Pear","grape","apple","banana","orange","watermelon","Pear","grape"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 建立适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);//对list_view 设置适配器
- 一般用法
- 建立一个fruit类
package com.example.peipeng.intent;
public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name = name;
this.imageId = imageId;
}
public String getName(){
return this.name;
}
public int getImageId(){
return this.imageId;
}
}
- 建立一个listView的自定义布局,被用于adapter定义页面布局,包含图片和文字描述
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="100dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/fruit_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- 建立fruit_adapter,将ListView和data之间建立连接
package com.example.peipeng.intent;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resorceId;
public FruitAdapter(Context context, int textViewResorceId, List<Fruit> objects){
super(context,textViewResorceId,objects);
resorceId = textViewResorceId;
}
@Override
public View getView(int position, View converView, ViewGroup parent){
Fruit fruit = getItem(position);// 获取当前位置的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resorceId,parent,false);
ImageView fruitImage = view.findViewById(R.id.fruit_image);
TextView fruitText = view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitText.setText(fruit.getName());
return view;
}
}
- 主活动界面中建立ListView控件
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
- 主活动中实例化Fruit ,且设置好adapter
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
// 程序运行不出来的原因,活动中界面listView和数据不匹配,注意一点:adapter和页面匹配,不是和页面中的listView匹配
private void initFruits(){
for (int i = 0;i<20 ; i++){
Fruit apple = new Fruit("Apple",R.drawable.apple1);//等下添加图片使用imageView的方法
fruitList.add(apple);
}
}