展示图片
使用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">
<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){ 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(); stringArrayAdapter = new ArrayAdapter<>( this, R.layout.item_st, R.id.item_st_tv, mDatas); typeLv.setAdapter(stringArrayAdapter); stringArrayAdapter1 = new ArrayAdapter<>(this, R.layout.item_st_gv, R.id.item_gtv, mDatas); typeGv.setAdapter(stringArrayAdapter1); stringArrayAdapter2 = new ArrayAdapter<>(this, R.layout.item_st_sp, R.id.item_st_tv, spArr); typeSp.setAdapter(stringArrayAdapter2); }
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); } }
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">
<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">
<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;
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; }
@Override public int getCount() { return mDatas.size(); }
@Override public Object getItem(int pos) { return mDatas.get(pos); }
@Override public long getItemId(int pos) { return pos; }
@Override public View getView(int pos, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(context) .inflate(R.layout.item_text, parent, false); holder = new ViewHolder(convertView); 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; }
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); }
} }
|