GridView
表格视图。
MainActivity.java
package com.example.gridview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 表格布局
*/
// 处理添加数据
String[] title = new String[]{"aci","ban","bon","egg","fu","gift","light","tip"};
int[] img = new int[]{R.drawable.acichole, R.drawable.banboo, R.drawable.bong, R.drawable.egg, R.drawable.fu, R.drawable.gift, R.drawable.light, R.drawable.tip};
List<Map<String, Object>> list = new ArrayList<>();
for (int i=0;i<title.length;i++){
Map<String,Object> map = new HashMap<>();
map.put("name", title[i]);
map.put("img", img[i]);
list.add(map);
}
GridView gridView = findViewById(R.id.gridView);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list,R.layout.grid_item, new String[]{"img","name"}, new int[]{R.id.imageView, R.id.textView});
gridView.setAdapter(simpleAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, title[position], Toast.LENGTH_SHORT).show();
}
});
}
}
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"
tools:context=".MainActivity">
<GridView
android:numColumns="4"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
grid_item.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:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="120px"
android:layout_height="120px"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
效果图:
calendarView
mainactivity.java
/**
* 日历部分
*/
CalendarView calendarView = findViewById(R.id.calendarView);
Button button = findViewById(R.id.button);
// 初始时间
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
time = format.format(calendarView.getDate());
// 获取选中时间
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
time = year+"年"+month+"月"+dayOfMonth+"日";
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
});
activity_main.xml
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="enter" />
效果图:
DatePicker
另一种日历控件,不过默认给出的可能没有,需要手动把代码敲上去。
与calendar的区别是在日历表上方增加了一个显示日期的视图。这里就不演示了。