展示图片

使用AppCompatImageView标签展示图片,放置图片的控件
src: 设置显示在控件上的图片
app:srcCompat=”” 可以放置显示图片(不会显示),设计时展示,运行时不展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/purple_200">
<!-- 放置图片的控件
src: 设置显示在控件上的图片
app:srcCompat="" 可以放置显示图片(不会显示),设计时展示,运行时不展示
-->
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/title_iv_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:src="@mipmap/icon_back"/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/title_iv_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:src="@mipmap/icon_setting"/>
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="18sp"
android:textStyle="bold"
android:text="日常用品"/>
</RelativeLayout>

引用布局

使用include引用其他布局,示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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">

<!-- 引用其他布局 -->
<include
android:id="@+id/main_title"
layout="@layout/title_layout"/>

</LinearLayout>

单选按钮

RadioGroup 单选按钮组
RadioButton 单选按钮

布局示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_below="@+id/tv"
android:layout_margin="10dp"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/rb_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:id="@+id/rb_qt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其他"/>

</RadioGroup>

单选按钮点击事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//单选按钮监听器
RadioGroup.OnCheckedChangeListener changeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
//如果一个页面当中有多个单选按钮,可以判断一下哪个组被触发了
if(radioGroup.getId() == R.id.rg){
//选中的单选按钮的id
switch (id){
case R.id.rb_man:
showToast("您选择了男性");
break;
case R.id.rb_woman:
showToast("您选择了女性");
break;
case R.id.rb_qt:
showToast("您选择了其他");
break;
}
}
}
};

activity跳转

结束当前avtivity: finish()

activity跳转是压栈过程,打开新的活动,相当于往栈中push一个新的活动对象。结束活动,相当于pop出栈

视图

ListView, 使用适配器ArrayAdapter进行绑定设置。示例

activity_shop_type.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ShopTypeActivity">
<include layout="@layout/title_layout"
android:id="@+id/st_title"></include>
<!-- 下拉列表视图-->
<Spinner
android:id="@+id/st_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_sp"
android:dropDownWidth="match_parent"
android:spinnerMode="dropdown"
android:overlapAnchor="false"
android:layout_margin="10dp"/>
<!-- 列表视图-->
<ListView
android:id="@+id/st_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/teal_200"
android:dividerHeight="1dp"
android:visibility="gone"/>
<GridView
android:id="@+id/st_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"/>

</LinearLayout>

ShopTypeActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.xyl.app1104;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class ShopTypeActivity extends AppCompatActivity {
ListView typeLv;
GridView typeGv;
List<String> mDatas; //数据源
Spinner typeSp;
String[] spArr = {"列表视图", "网格视图"};
private ArrayAdapter<String> stringArrayAdapter;
private ArrayAdapter<String> stringArrayAdapter1;
private ArrayAdapter<String> stringArrayAdapter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_type);
initView();
//获取数据源
initDatas();
//设置适配器 ArrayAdapter:当列表或者网格当中的每一项只有一个文本框,可以使用这个适配器
//参数2:每一项的item的布局 参数3:显示数据的文本 参数4:数据
stringArrayAdapter = new ArrayAdapter<>(
this, R.layout.item_st, R.id.item_st_tv, mDatas);
typeLv.setAdapter(stringArrayAdapter);
//给gridview设置adapter
stringArrayAdapter1 = new ArrayAdapter<>(this, R.layout.item_st_gv,
R.id.item_gtv, mDatas);
typeGv.setAdapter(stringArrayAdapter1);
//给Spinner设置adapter
stringArrayAdapter2 = new ArrayAdapter<>(this, R.layout.item_st_sp, R.id.item_st_tv, spArr);
typeSp.setAdapter(stringArrayAdapter2);
}

/**
* 对于ListView的数据源进行初始化
*/
private void initDatas() {
mDatas = new ArrayList<>();
mDatas.add("调料干货");
mDatas.add("零食");
mDatas.add("饮料");
mDatas.add("厨房用品");
mDatas.add("日用品");
mDatas.add("烟酒");
for(int i = 1; i < 20; i++){
mDatas.add("第" + i + "条数据");
}
}

private void initView() {
typeLv = findViewById(R.id.st_lv);
typeGv = findViewById(R.id.st_gv);
typeSp = findViewById(R.id.st_sp);
}
}

/**
* 对于ListView的数据源进行初始化
*/
private void initDatas() {
mDatas = new ArrayList<>();
mDatas.add("调料干货");
mDatas.add("零食");
mDatas.add("饮料");
mDatas.add("厨房用品");
mDatas.add("日用品");
mDatas.add("烟酒");
for(int i = 1; i < 20; i++){
mDatas.add("第" + i + "条数据");
}
}

private void initView() {
typeLv = findViewById(R.id.st_lv);
typeGv = findViewById(R.id.st_gv);
}
}

列表视图

item_st.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:padding="20dp"
android:layout_height="match_parent">
<!-- listView中每一条的布局-->
<TextView
android:id="@+id/item_st_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:text="酒水饮料"/>
<ImageView
android:id="@+id/item_st_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@mipmap/icon_enter"/>


</RelativeLayout>

网格视图

gridView

item_st_gv.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/bg_gitem"
android:layout_margin="5dp">
<TextView
android:id="@+id/item_gtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="20sp"
android:text="111"/>

</RelativeLayout>

drawable/bg_gitem.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/>
<stroke android:width="3dp" android:color="@color/teal_700"/>
<solid android:color="@color/pink2"/>

</shape>

下拉列表视图

Spinner

设置下拉列表不遮挡下拉框可以使用

android:overlapAnchor="false" 这条属性代码提示没有

layout/item_st_sp.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="match_parent">
<!-- listView中每一条的布局-->
<TextView
android:id="@+id/item_st_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:text="酒水饮料"/>



</RelativeLayout>

自定义适配器

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.xyl.app1104;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
* ListView的适配器
* 因为每一个item的布局不只有一个TextView,所以要自己写适配器
* 1. 继承于BaseAdapter
* 2. 重写4个方法,不需要自己调用,
*/
public class MyAdapter extends BaseAdapter {
//显示的数据源
private List<ItemBean> mDatas;
private Context context;

public MyAdapter(Context context, List<ItemBean> mDatas) {
this.mDatas = mDatas;
this.context = context;
}

/**
* 返回列表的长度
* @return
*/
@Override
public int getCount() {
return mDatas.size();
}

/**
* 返回列表指定位置对应的数据
* @param pos
* @return
*/
@Override
public Object getItem(int pos) {
return mDatas.get(pos);
}

/**
* 返回指定位置的itemView的id
* @param pos
* @return
*/
@Override
public long getItemId(int pos) {
return pos;
}

/**
* 返回指定位置的View对象,将布局转换成view对象,view中显示的内容都在这里设置
* convertView:在ListView当中可以被复用的itemView
* itembean是对象,在集合当中,如果集合有1000条就有1000个itembean的对象
* 将itembean对象的内容设置到view上,然后让view再放入listview中
* 如果有1000个itemview对象放到1000个view上就会资源浪费。因为用户在一个屏幕当中
* 只有几条,当滑动屏幕时,上面的view内容就没用了。无用的view的格式和后面一样。
* 所以后面就不需要再生成view对象了,就可以直接使用了。
* @param pos
* @param convertView
* @param parent
* @return
*/
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//将布局转换成view对象 LayoutInflater:布局加载器
convertView = LayoutInflater.from(context)
.inflate(R.layout.item_text, parent, false);
//进行控件属性的设置
holder = new ViewHolder(convertView);
//将holder对象绑定在view上
convertView.setTag(holder);
}
else{
//属性已经都找到了,不用重新设置
holder = (ViewHolder) convertView.getTag();
}
ItemBean bean = mDatas.get(pos);
holder.tv1.setText(bean.getTitle());
holder.tv2.setText(bean.getMsg());
holder.iv.setImageResource(bean.getResId());

return convertView;
}

/**
* 将每一个itemview对象当中包含所有需要改变的view放到同一个部分进行保管
*/
class ViewHolder{
TextView tv1, tv2;
ImageView iv;
public ViewHolder(View itemView){
tv1 = itemView.findViewById(R.id.item_tv1);
tv2 = itemView.findViewById(R.id.item_tv2);
iv = itemView.findViewById(R.id.item_iv);
}

}
}
/**
* ListView的优化
* 1. 设置高度固定,避免重复测量显示的高度
* 2. getView的优化
* 对于convertView进行判断,如果有创建好的,闲置的view模板,可以直接使用,否则就创建
* 对于findViewById进行优化,对于可复用的view,不需要重复查找控件id,对于新生成的,查找控件id
*/