1.实体类
1.1 商品实体
public class GoodsInfo {
public int id;
// 名称
public String name;
// 描述
public String description;
// 价格
public float price;
// 大图的保存路径
public String picPath;
// 大图的资源编号
public int pic;
// 声明一个手机商品的名称数组
private static String[] mNameArray = {
"iPhone11", "Mate30", "小米10", "OPPO Reno3", "vivo X30", "荣耀30S"
};
// 声明一个手机商品的描述数组
private static String[] mDescArray = {
"Apple iPhone11 256GB 绿色 4G全网通手机",
"华为 HUAWEI Mate30 8GB+256GB 丹霞橙 5G全网通 全面屏手机",
"小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机",
"OPPO Reno3 8GB+128GB 蓝色星夜 双模5G 拍照游戏智能手机",
"vivo X30 8GB+128GB 绯云 5G全网通 美颜拍照手机",
"荣耀30S 8GB+128GB 蝶羽红 5G芯片 自拍全面屏手机"
};
// 声明一个手机商品的价格数组
private static float[] mPriceArray = {6299, 4999, 3999, 2999, 2998, 2399};
// 声明一个手机商品的大图数组
private static int[] mPicArray = {
R.drawable.iphone, R.drawable.huawei, R.drawable.xiaomi,
R.drawable.oppo, R.drawable.vivo, R.drawable.rongyao
};
// 获取默认的手机信息列表
public static ArrayList<GoodsInfo> getDefaultList() {
ArrayList<GoodsInfo> goodsList = new ArrayList<GoodsInfo>();
for (int i = 0; i < mNameArray.length; i++) {
GoodsInfo info = new GoodsInfo();
info.id = i;
info.name = mNameArray[i];
info.description = mDescArray[i];
info.price = mPriceArray[i];
info.pic = mPicArray[i];
goodsList.add(info);
}
return goodsList;
}
@Override
public String toString() {
return "GoodsInfo{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", price=" + price +
", picPath='" + picPath + '\'' +
", pic=" + pic +
'}';
}
}
1.2 购物车实体
//购物车信息
public class CartInfo {
public int id;
// 商品编号
public int goodsId;
// 商品数量
public int count;
public CartInfo(){}
public CartInfo(int id, int goodsId, int count) {
this.id = id;
this.goodsId = goodsId;
this.count = count;
}
}
2. 持久层
public class ShoppingDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "shopping.db";
private static final int DB_VERSION = 1;
private static final String TABLE_GOODS_INFO = "goods_info";
private static final String TABLE_CART_INFO = "cart_info";
private static ShoppingDBHelper mHelper = null;
private SQLiteDatabase mRDB = null;
private SQLiteDatabase mWDB = null;
public ShoppingDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public static ShoppingDBHelper getInstance(Context context) {
if (mHelper == null) {
mHelper = new ShoppingDBHelper(context);
}
return mHelper;
}
public SQLiteDatabase openReadLink() {
if (mRDB == null || !mRDB.isOpen()) {
mRDB = mHelper.getReadableDatabase();
}
return mRDB;
}
public SQLiteDatabase openWriteLink() {
if (mWDB == null || !mWDB.isOpen()) {
mWDB = mHelper.getWritableDatabase();
}
return mWDB;
}
public void closeLink() {
if (mRDB != null && mRDB.isOpen()) {
mRDB.close();
mRDB = null;
}
if (mWDB != null && mWDB.isOpen()) {
mWDB.close();
mWDB = null;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_GOODS_INFO +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" name VARCHAR NOT NULL," +
" description VARCHAR NOT NULL," +
" price FLOAT NOT NULL," +
" pic_path VARCHAR NOT NULL);";
db.execSQL(sql);
// 创建购物车信息表
sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CART_INFO +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" goods_id INTEGER NOT NULL," +
" count INTEGER NOT NULL);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
String sql = "ALTER TABLE " + TABLE_GOODS_INFO + " ADD COLUMN phone VARCHAR;";
db.execSQL(sql);
sql = "ALTER TABLE " + TABLE_CART_INFO + " ADD COLUMN password VARCHAR;";
db.execSQL(sql);
}
public long deleteByName() {
//删除所有
return mWDB.delete(TABLE_GOODS_INFO, "1=1", null);
}
public void insertGoodsInfos(List<GoodsInfo> list){
//插入多条记录,要么全部成功,要么全部失败
try{
mWDB.beginTransaction();
for(GoodsInfo info:list){
ContentValues values = new ContentValues();
values.put("name",info.name);
values.put("description",info.description);
values .put("price",info.price);
values.put("pic_path",info.picPath);
mWDB.insert(TABLE_GOODS_INFO,null,values);
}
mWDB.setTransactionSuccessful();
}catch (Exception e){
e.printStackTrace();
}finally {
mWDB.endTransaction();
}
}
public List<GoodsInfo> queryAllGoodsInfo(){
String sql = "select * from " + TABLE_GOODS_INFO;
List<GoodsInfo> list = new ArrayList<>();
Cursor cursor = mRDB.rawQuery(sql, null);
while (cursor.moveToNext()) {
GoodsInfo info = new GoodsInfo();
info.id = cursor.getInt(0);
info.name = cursor.getString(1);
info.description = cursor.getString(2);
info.price = cursor.getFloat(3);
info.picPath = cursor.getString(4);
list.add(info);
}
cursor.close();
return list;
}
public void insertCartInfo(int goodsId) {
CartInfo cartInfo = queryCartInfoByGoodsId(goodsId);
ContentValues values = new ContentValues();
values.put("goods_id",goodsId);
if(cartInfo==null){
values.put("count",1);
mWDB.insert(TABLE_CART_INFO,null,values);
}else{
values.put("_id",cartInfo.id);
values.put("count",++cartInfo.count);
mWDB.update(TABLE_CART_INFO,values,"_id=?",new String[]{String.valueOf(cartInfo.id)});
}
}
private CartInfo queryCartInfoByGoodsId(int goodsId) {
Cursor cursor = mRDB.query(TABLE_CART_INFO, null, "goods_id=?",
new String[]{String.valueOf(goodsId)}, null, null, null);
CartInfo info=null;
if (cursor.moveToNext()){
info=new CartInfo();
info.id=cursor.getInt(0);
info.goodsId=cursor.getInt(1);
info.count=cursor.getInt(2);
}
return info;
}
public int countCartInfo() {
String sql = "select sum(count) from " + TABLE_CART_INFO;
Cursor cursor = mRDB.rawQuery(sql, null);
int count = 0;
if (cursor.moveToNext()) {
count = cursor.getInt(0);
}
cursor.close();
return count;
}
// 查询购物车中所有的信息列表
public List<CartInfo> queryAllCartInfo() {
List<CartInfo> list = new ArrayList<>();
Cursor cursor = mRDB.query(TABLE_CART_INFO, null, null, null, null, null, null);
while (cursor.moveToNext()) {
CartInfo info = new CartInfo();
info.id = cursor.getInt(0);
info.goodsId = cursor.getInt(1);
info.count = cursor.getInt(2);
list.add(info);
}
return list;
}
public GoodsInfo queryGoodsInfoById(int goodsId) {
GoodsInfo info = null;
Cursor cursor = mRDB.query(TABLE_GOODS_INFO, null, "_id=?",
new String[]{String.valueOf(goodsId)}, null, null, null);
if (cursor.moveToNext()) {
info = new GoodsInfo();
info.id = cursor.getInt(0);
info.name = cursor.getString(1);
info.description = cursor.getString(2);
info.price = cursor.getFloat(3);
info.picPath = cursor.getString(4);
}
return info;
}
public void deleteCartInfoByGoodsId(int goodsId) {
mWDB.delete(TABLE_CART_INFO, "goods_id=?", new String[]{String.valueOf(goodsId)});
}
public void deleteAllCartInfo(){
mWDB.delete(TABLE_CART_INFO, "1=1", null);
}
}
3. 业务层
3.1 商品页面
public class ShoppingChannelActivity extends AppCompatActivity implements View.OnClickListener {
private ShoppingDBHelper mDBHelper;
private TextView tv_count;
private GridLayout gl_channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_channel);
mDBHelper = ShoppingDBHelper.getInstance(this);
TextView tv_title = findViewById(R.id.tv_title);
tv_title.setText("手机商场");
gl_channel = findViewById(R.id.gl_channel);
tv_count = findViewById(R.id.tv_count);
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.iv_cart).setOnClickListener(this);
mDBHelper.openReadLink();
mDBHelper.openWriteLink();
showGoods();
}
@Override
protected void onResume() {
super.onResume();
//查询购物车商品总数,并展示
showCartInfoTotal();
}
private void showCartInfoTotal() {
int count= mDBHelper.countCartInfo();
MyApplication.getInstance().goodsCount=count;
tv_count.setText(String.valueOf(count));
}
private void showGoods() {
//获取屏幕的宽度
int screenWidth = getResources().getDisplayMetrics().widthPixels;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
List<GoodsInfo> list = mDBHelper.queryAllGoodsInfo();
gl_channel.removeAllViews();
for (GoodsInfo Info : list) {
View view = LayoutInflater.from(this).inflate(R.layout.item_goods, null);
ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
TextView tv_name =view.findViewById(R.id.tv_name);
Button btn_add = view.findViewById(R.id.btn_add);
TextView tv_price = view.findViewById(R.id.tv_price);
//给控件设置值
iv_thumb.setImageURI(Uri.parse(Info.picPath));
tv_name.setText(Info.name);
tv_price.setText(String.valueOf((int)Info.price));
btn_add.setOnClickListener(v->{
addToCart(Info.id,Info.name);
});
iv_thumb.setOnClickListener(v->{
Intent intent = new Intent(this, ShoppingDetailActivity.class);
intent.putExtra("goods_id",Info.id);
startActivity(intent);
});
gl_channel.addView(view,layoutParams);
}
}
private void addToCart(int goodsId, String name) {
int count = ++MyApplication.getInstance().goodsCount;
tv_count.setText(String.valueOf(count));
mDBHelper.insertCartInfo(goodsId);
ToastUtil.show(this,"已添加商品"+name+"到购物车");
}
@Override
protected void onDestroy() {
super.onDestroy();
mDBHelper.closeLink();
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.iv_back){
finish();
}else if(view.getId()==R.id.iv_cart){
// 点击了购物车图标
// 从商场页面跳到购物车页面
Intent intent = new Intent(this, ShoppingCartActivity.class);
// 设置启动标志,避免多次返回同一页面的
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
3.2 商品详情页面
public class ShoppingDetailActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_title;
private TextView tv_count;
private TextView tv_goods_price;
private TextView tv_goods_desc;
private ImageView iv_goods_pic;
private ShoppingDBHelper mDBHelper;
private int mGoodsId;
private int goodsId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_detail);
tv_title = findViewById(R.id.tv_title);
tv_count = findViewById(R.id.tv_count);
tv_goods_price = findViewById(R.id.tv_goods_price);
tv_goods_desc = findViewById(R.id.tv_goods_desc);
iv_goods_pic = findViewById(R.id.iv_goods_pic);
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.iv_cart).setOnClickListener(this);
findViewById(R.id.btn_add_cart).setOnClickListener(this);
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
mDBHelper = ShoppingDBHelper.getInstance(this);
}
@Override
protected void onResume() {
super.onResume();
showDetail();
}
private void showDetail() {
goodsId = getIntent().getIntExtra("goods_id", 0);
if (goodsId > 0) {
GoodsInfo goodsInfo = mDBHelper.queryGoodsInfoById(goodsId);
if (goodsInfo != null) {
tv_title.setText(goodsInfo.name);
tv_goods_price.setText(String.valueOf((int) goodsInfo.price));
tv_goods_desc.setText(goodsInfo.description);
iv_goods_pic.setImageURI(Uri.parse(goodsInfo.picPath));
}
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.iv_back) {
finish();
} else if (view.getId() == R.id.iv_cart) {
Intent intent = new Intent(this, ShoppingCartActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_add_cart) {
addToCart(goodsId);
}
}
private void addToCart(int goodsId) {
int count=++MyApplication.getInstance().goodsCount;
tv_count.setText(String.valueOf(count));
mDBHelper.insertCartInfo(goodsId);
ToastUtil.show(this,"以添加商品到购物车");
}
}
3.3 购物车页面
public class ShoppingCartActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_count;
private LinearLayout ll_cart;
private ShoppingDBHelper mDBHelper;
// 声明一个购物车中的商品信息列表
private List<CartInfo> mCartList;
// 声明一个根据商品编号查找商品信息的映射,把商品信息缓存起来,这样不用每一次都去查询数据库
private Map<Integer, GoodsInfo> mGoodsMap = new HashMap<>();
private TextView tv_total_price;
private LinearLayout ll_empty;
private LinearLayout ll_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
TextView tv_title = findViewById(R.id.tv_title);
tv_title.setText("购物车");
ll_cart = findViewById(R.id.ll_cart);
tv_total_price = findViewById(R.id.tv_total_price);
tv_count = findViewById(R.id.tv_count);
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
mDBHelper = ShoppingDBHelper.getInstance(this);
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.btn_shopping_channel).setOnClickListener(this);
findViewById(R.id.btn_clear).setOnClickListener(this);
findViewById(R.id.btn_settle).setOnClickListener(this);
ll_empty = findViewById(R.id.ll_empty);
ll_content = findViewById(R.id.ll_content);
}
@Override
protected void onResume() {
super.onResume();
showCart();
}
// 展示购物车中的商品列表
private void showCart() {
// 移除下面的所有子视图
ll_cart.removeAllViews();
// 查询购物车数据库中所有的商品记录
mCartList = mDBHelper.queryAllCartInfo();
if (mCartList.size() == 0) {
return;
}
for (CartInfo info : mCartList) {
// 根据商品编号查询商品数据库中的商品记录
GoodsInfo goods = mDBHelper.queryGoodsInfoById(info.goodsId);
mGoodsMap.put(info.goodsId, goods);
Log.d("lzk",goods.toString());
// 获取布局文件item_cart.xml的根视图
View view = LayoutInflater.from(this).inflate(R.layout.item_cart, null);
ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
TextView tv_name = view.findViewById(R.id.tv_name);
TextView tv_desc = view.findViewById(R.id.tv_desc);
TextView tv_count = view.findViewById(R.id.tv_count);
TextView tv_price = view.findViewById(R.id.tv_price);
TextView tv_sum = view.findViewById(R.id.tv_sum);
iv_thumb.setImageURI(Uri.parse(goods.picPath));
tv_name.setText(goods.name);
tv_desc.setText(goods.description);
tv_count.setText(String.valueOf(info.count));
tv_price.setText(String.valueOf((int) goods.price));
// 设置商品总价
tv_sum.setText(String.valueOf((int) (info.count * goods.price)));
view.setOnLongClickListener(v->{
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingCartActivity.this);
builder.setMessage("是否从购物车中删除"+goods.name+"?");
builder.setPositiveButton("确定", (dialog, which) -> {
//移除当前视图
ll_cart.removeView(v);
// 删除购物车中的商品记录
deleteGoods(info);
});
builder.setNegativeButton("取消", null);
builder.create().show();
return true;
});
view.setOnClickListener(v->{
Intent intent = new Intent(ShoppingCartActivity.this, ShoppingDetailActivity.class);
intent.putExtra("goods_id", goods.id);
startActivity(intent);
});
// 往购物车列表添加该商品行
ll_cart.addView(view);
}
// 重新计算购物车中的商品总金额
refreshTotalPrice();
}
private void deleteGoods(CartInfo info) {
MyApplication.getInstance().goodsCount-=info.count;
mDBHelper.deleteCartInfoByGoodsId(info.goodsId);
CartInfo removed=null;
for (CartInfo cartInfo : mCartList) {
if(cartInfo.goodsId==info.goodsId){
removed=cartInfo;
break;
}
}
mCartList.remove(removed);
showCount();
ToastUtil.show(this,"已从购物车删除"+mGoodsMap.get(info.goodsId).name);
mGoodsMap.remove(info.goodsId);
refreshTotalPrice();
}
// 显示购物车图标中的商品数量
private void showCount() {
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
// 购物车中没有商品,显示“空空如也”
if (MyApplication.getInstance().goodsCount == 0) {
ll_empty.setVisibility(View.VISIBLE);
ll_content.setVisibility(View.GONE);
ll_cart.removeAllViews();
} else {
ll_content.setVisibility(View.VISIBLE);
ll_empty.setVisibility(View.GONE);
}
}
// 重新计算购物车中的商品总金额
private void refreshTotalPrice() {
int totalPrice = 0;
for (CartInfo info : mCartList) {
GoodsInfo goods = mGoodsMap.get(info.goodsId);
totalPrice += goods.price * info.count;
}
tv_total_price.setText(String.valueOf(totalPrice));
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.iv_back){
finish();
}else if(view.getId()==R.id.btn_shopping_channel){
Intent intent=new Intent(this,ShoppingChannelActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(view.getId()==R.id.btn_clear){
mDBHelper.deleteAllCartInfo();
MyApplication.getInstance().goodsCount=0;
showCount();
ToastUtil.show(this,"已清空购物车");
}else if(view.getId()==R.id.btn_settle){
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingCartActivity.this);
builder.setTitle("结算商品");
builder.setMessage("是否结算商品?");
builder.setPositiveButton("确定",null);
builder.create().show();
}
}
}
4. Application
public class MyApplication extends Application {
//购物车中的商品数量
public int goodsCount;
private static MyApplication mApp;
public static MyApplication getInstance() {
return mApp;
}
@Override
public void onCreate() {
super.onCreate();
mApp=this;
initGoodInfo();
}
private void initGoodInfo() {
// 获取共享参数保存的是否首次打开参数
boolean isFirst = SharedUtil.getInstance(this).redaBoolean("first", true);
// 获取当前App的私有下载路径
String directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separatorChar;
if (isFirst) {
ShoppingDBHelper dbHelper = ShoppingDBHelper.getInstance(this);
// 模拟网络图片下载
List<GoodsInfo> list = GoodsInfo.getDefaultList();
for (GoodsInfo info : list) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), info.pic);
String path = directory + info.id + ".jpg";
// 往存储卡保存商品图片
FileUtil.saveImage(path, bitmap);
// 回收位图对象
bitmap.recycle();
info.picPath = path;
}
// 打开数据库,把商品信息插入到表中
dbHelper.openWriteLink();
dbHelper.insertGoodsInfos(list);
dbHelper.closeLink();
// 把是否首次打开写入共享参数
SharedUtil.getInstance(this).writeBoolean("first", false);
}
}
}
5. 前端页面
5.1 商品展示页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/orange"
android:orientation="vertical">
<include layout="@layout/title_shopping" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout
android:id="@+id/gl_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2" />
</ScrollView>
</LinearLayout>
5.2 商品详情页面
<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:background="@color/orange"
android:orientation="vertical">
<include layout="@layout/title_shopping" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_goods_pic"
android:layout_width="match_parent"
android:layout_height="350dp"
android:scaleType="fitCenter"
tools:src="@drawable/xiaomi" />
<TextView
android:id="@+id/tv_goods_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/red"
android:textSize="22sp"
tools:text="1990" />
<TextView
android:id="@+id/tv_goods_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/black"
android:textSize="15sp"
tools:text="小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机" />
<Button
android:id="@+id/btn_add_cart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加入购物车"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
5.3 商品购物车页面
<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:background="@color/orange"
android:orientation="vertical">
<include layout="@layout/title_shopping" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="85dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="图片"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="名称"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="数量"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="单价"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="总价"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_cart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="清空"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|right"
android:text="总金额:"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="center|left"
android:textColor="@color/red"
android:textSize="25sp" />
<Button
android:id="@+id/btn_settle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="结算"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="哎呀,购物车空空如也,快去选购商品吧"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn_shopping_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="逛逛手机商场"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
6. 界面展示
![](https://i-blog.csdnimg.cn/direct/ecacd8a46de442d3b54278f36d8e2364.png)
![](https://i-blog.csdnimg.cn/direct/6b724f867f204158bbd20aa85905396a.png)
![](https://i-blog.csdnimg.cn/direct/05279ab9958344448a9085d763657994.png)