Android?完整購物商城界面的實(shí)現(xiàn)案例
activity_main.xml
在res/layout文件中,放置一個(gè)TextView控件用于顯示購物商城界面的標(biāo)題,放置一個(gè)ListView控件用于顯示購物商場界面的列表
<?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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="購物商城"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:background="#FF8F03"
android:gravity="center"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@color/colorAccent" />
</LinearLayout>
list_Item.xml
在res/layout文件中創(chuàng)建一個(gè)列表?xiàng)l目界面的布局文件list_item.xml,在該文件中放置一個(gè)Image View控件用于顯示商品的圖片;放置2個(gè)TextView控件分別用于顯示商品的名稱和價(jià)格!
<?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="match_parent"
android:padding="16dp">
<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="90dp"
android:layout_centerVertical="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="桌子"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="10dp"
android:text="價(jià)格:"
android:textColor="#FF8F03"
android:textSize="20sp" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/tv_price"
android:text="1000"
android:textColor="#FF8F03"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java
12-14行代碼定義了數(shù)組titles,prices,icons,分別用于存儲(chǔ)商品列表中的商品名稱,價(jià)格和圖片,并且3個(gè)數(shù)組的長度一致。
19行創(chuàng)建MyBaseAdpter的實(shí)例
20行設(shè)置數(shù)據(jù)適配器List View上
23-34行代碼返回條目的長度,價(jià)格,數(shù)據(jù)對(duì)象
37行找到list_item.xml中創(chuàng)建的TextView控件
創(chuàng)建一個(gè)View Holder類,將需要加載的控件變量放在該類中
最后調(diào)用id屬性,set實(shí)現(xiàn)!
package com.example.dymemory1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView mListView;
private String[] titles = {"蘋果","蛋糕","修羅壁紙","桌子","獼猴桃","毛衣"};
private String[] prices = {"10元/kg","100元/個(gè)","50元/張","45元/把","10元/kg","110元/件"};
private int[] icons = {R.drawable.apple,R.drawable.cake,R.drawable.juyoujing,R.drawable.table,R.drawable.kiwifruit,R.drawable.wireclothes};
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
mListView=(ListView)findViewById(R.id.lv);
MyBaseAdaPter mAdapter = new MyBaseAdaPter();
mListView.setAdapter(mAdapter);
}
class MyBaseAdaPter extends BaseAdapter{
@Override
public int getCount( ){
return titles.length;
}
@Override
public Object getItem(int position){
return titles[position];
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
ViewHolder holder =null;
if(convertView == null){
convertView = View.inflate( MainActivity.this,R.layout.list_item,null);
holder= new ViewHolder();
holder.title=(TextView) convertView.findViewById(R.id.title);
holder.price=(TextView) convertView.findViewById(R.id.price);
holder.iv=(ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(titles[position]);
holder.price.setText(prices[position]);
holder.iv.setBackgroundResource(icons[position]);
return convertView;
}
}
static class ViewHolder{
TextView title;
TextView price;
ImageView iv;
}
}
到此這篇關(guān)于Android 完整購物商城界面的實(shí)現(xiàn)案例的文章就介紹到這了,更多相關(guān)Android 購物商城界面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中RecyclerView實(shí)現(xiàn)分頁滾動(dòng)的方法詳解
RecyclerView實(shí)現(xiàn)滾動(dòng)相信對(duì)大家來說都不陌生,但是本文主要給大家介紹了利用Android中RecyclerView實(shí)現(xiàn)分頁滾動(dòng)的思路和方法,可以實(shí)現(xiàn)翻頁功能,一次翻一頁,也可以實(shí)現(xiàn)翻至某一頁功能。文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04
Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
android設(shè)備不識(shí)別awk命令 缺少busybox怎么辦
這篇文章主要為大家詳細(xì)介紹了android設(shè)備不識(shí)別awk命令,缺少busybox的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
基于Android中手勢交互的實(shí)現(xiàn)方法
本篇文章是對(duì)Android中手勢交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
Android小程序?qū)崿F(xiàn)個(gè)人信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)個(gè)人信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
android開發(fā)教程之用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小
用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小的方法,,需要的朋友可以參考下2014-02-02
android開發(fā)教程之使用looper處理消息隊(duì)列
這篇文章主要介紹了通過HandlerThread對(duì)象來實(shí)現(xiàn)使用looper處理消息隊(duì)列的功能,大家參考使用吧2014-01-01

