1.2020/7/19开始
做个记录,学习Android
开发环境:
jdk:1.8
Android studio 4.0
一、首先,建立项目工程(按照下图操作)
二、建立好之后,会默认打开我们所创建的工程,我们按照如下操作
1、导入所需的jar文件
2、编写颜色资源文件
3、创建工程项目结构
三、首页设计
一)标题栏设计
1、新建标题布局文件
a:编写内容:
title_bar.xml
<?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="50dp"
android:background="@color/blue_color"
>
<ImageView
android:layout_centerVertical="true"
android:id="@+id/iv_back"
android:clickable="true"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@drawable/iv_back_selector"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
android:text="首页"
android:textColor="@android:color/white"
android:textSize="25dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
2、创建点击事件选择器
iv_back_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/iv_back" android:state_pressed="false"/>
<item android:drawable="@drawable/iv_back_selected" android:state_pressed="true"/>
</selector>
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=".activity.MainActivity">
<include layout="@layout/title_bar"/>
</LinearLayout>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
自此首页标题栏布局完成。
二)内容页设计
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"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<include layout="@layout/title_bar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="fitXY"
android:src="@drawable/banner"/>
<com.example.orderproject.view.ShopListView
android:id="@+id/iv_shop_list"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
mainactivity.java
package com.example.orderproject.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.orderproject.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView tvTitle;
private ListView ivshopList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvTitle=findViewById(R.id.tv_title);
ivshopList=findViewById(R.id.iv_shop_list);
List<String> list=new ArrayList<>();
for (int i=0;i<20;i++){
list.add(i+"-"+i);
}
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,list.toArray());
ivshopList.setAdapter(adapter);
}
}
新建view文件
ShopListView.java
package com.example.orderproject.view;
import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ListView;
public class ShopListView extends ListView {
public ShopListView(Context context) {
super(context);
}
public ShopListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ShopListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2,MeasureSpec.AT_MOST);
//VIEW的大小采用30位进行存储,那么我们应该给他30位的最大值,Int的最大值是32位
super.onMeasure(widthMeasureSpec,heightSpec);
}
}
效果图:
自此内容页布局结束
三)内容填充
创建工具文件。用于连接服务器并获取数据
装插件:gsonformat
gsonformat使用参考教程
装完之后要重新打开Android studio之后才会生效。
试下我们的接口地址:
复制接口全部内容。
然后再在我们的类下面去使用gsonformat。把我们上面的内容复制进去,就可以生成我们所需要的东西了。
这就是我们的生成的文件类了,有了这个东西就不需要自己去打对应的json的代码了。
package com.example.orderproject.bean;
import java.util.List;
public class ShopBean {
/**
* id : 1
* shopName : 蛋糕房
* saleNum : 996
* offerPrice : 100
* distributionCost : 5
* welfare : 进店可获得一个香草冰淇淋
* time : 配送约2-5小时
* shopPic : http://192.168.31.135:3000/order/img/shop/shop1.png
* shopNotice : 公告:下单后2-5小时送达!请耐心等候
* foodList : [{"foodId":"1","foodName":"招牌丰收硕果12寸","taste":"水果、奶油、面包、鸡蛋","saleNum":"50","price":198,"count":0,"foodPic":"http://192.168.31.135:3000/order/img/food/food1.png"},{"foodId":"2","foodName":"玫瑰花创意蛋糕","taste":"玫瑰花、奶油、鸡蛋","saleNum":"100","price":148,"count":0,"foodPic":"http://192.168.31.135:3000/order/img/food/food2.png"},{"foodId":"3","foodName":"布朗熊与可妮","taste":"奶油、巧克力、果粒夹层","saleNum":"80","price":90,"count":0,"foodPic":"http://192.168.31.135:3000/order/img/food/food3.png"}]
*/
private int id;
private String shopName;
private int saleNum;
private int offerPrice;
private int distributionCost;
private String welfare;
private String time;
private String shopPic;
private String shopNotice;
private List<FoodListBean> foodList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
public int getOfferPrice() {
return offerPrice;
}
public void setOfferPrice(int offerPrice) {
this.offerPrice = offerPrice;
}
public int getDistributionCost() {
return distributionCost;
}
public void setDistributionCost(int distributionCost) {
this.distributionCost = distributionCost;
}
public String getWelfare() {
return welfare;
}
public void setWelfare(String welfare) {
this.welfare = welfare;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getShopPic() {
return shopPic;
}
public void setShopPic(String shopPic) {
this.shopPic = shopPic;
}
public String getShopNotice() {
return shopNotice;
}
public void setShopNotice(String shopNotice) {
this.shopNotice = shopNotice;
}
public List<FoodListBean> getFoodList() {
return foodList;
}
public void setFoodList(List<FoodListBean> foodList) {
this.foodList = foodList;
}
public static class FoodListBean {
/**
* foodId : 1
* foodName : 招牌丰收硕果12寸
* taste : 水果、奶油、面包、鸡蛋
* saleNum : 50
* price : 198
* count : 0
* foodPic : http://192.168.31.135:3000/order/img/food/food1.png
*/
private String foodId;
private String foodName;
private String taste;
private String saleNum;
private int price;
private int count;
private String foodPic;
public String getFoodId() {
return foodId;
}
public void setFoodId(String foodId) {
this.foodId = foodId;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
public String getSaleNum() {
return saleNum;
}
public void setSaleNum(String saleNum) {
this.saleNum = saleNum;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getFoodPic() {
return foodPic;
}
public void setFoodPic(String foodPic) {
this.foodPic = foodPic;
}
}
}