Android實(shí)現(xiàn)簡(jiǎn)單卡片布局
GoogleNow是Android4.1全新推出的一款應(yīng)用他,它可以全面了解你的使用習(xí)慣,并為你提供現(xiàn)在或者未來(lái)可能用到的各種信息,GoogleNow提供的信息關(guān)聯(lián)度較高,幾乎是瞬間返回答案,總而言之,GoogleNow是Google提出的全新搜索概念。當(dāng)然,GoogleNow最為引人注目的當(dāng)屬它的卡片式設(shè)計(jì)。Google自家應(yīng)用紛紛采用卡片布局(Google Now,Google Plus,Google Play)。


在最新的QQ空間、新浪微博、豌豆莢中也可以見到卡片式設(shè)計(jì)的影子


下面介紹一種簡(jiǎn)單實(shí)現(xiàn)卡片布局的方式
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity" android:background="@drawable/radius_bg"> <ImageView android:id="@+id/iv_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_margin="8dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/iv_logo" android:layout_toRightOf="@id/iv_logo" android:text="@string/hello_world" android:textSize="16sp" /> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_name" android:layout_marginTop="5dp" android:layout_toRightOf="@id/iv_logo" android:text="@string/hello_world" android:textSize="13sp" /> </RelativeLayout>
自定義Shape圖片radius_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="3dp"/> <solid android:color="#ffffff"/> </shape>
主界面布局
<RelativeLayout 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" tools:context=".MainActivity" android:background="#e6e6e6"> <ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@android:color/transparent" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:dividerHeight="10dp" > </ListView> </RelativeLayout>
Card實(shí)體
package com.example.carduitest.model;
public class Card {
private String name;
private String desc;
private int icon;
public Card(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
自定義適配器
package com.example.carduitest.adapter;
import java.util.List;
import com.example.carduitest.R;
import com.example.carduitest.model.Card;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CardAdapter extends BaseAdapter {
private List<Card> data;
private Context context;
private LayoutInflater mInflater;
public CardAdapter(List<Card> data, Context context) {
this.data = data;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Card card = data.get(position);
holder.tv_name.setText(card.getName());
holder.tv_desc.setText(card.getDesc());
return convertView;
}
static class ViewHolder{
TextView tv_name;
TextView tv_desc;
}
}
package com.example.carduitest;
import java.util.ArrayList;
import java.util.List;
import com.example.carduitest.adapter.CardAdapter;
import com.example.carduitest.model.Card;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private List<Card> data = new ArrayList<Card>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
ListView mListView = (ListView) findViewById(R.id.mListView);
CardAdapter mAdapter = new CardAdapter(data,this);
mListView.setAdapter(mAdapter);
}
private void initData() {
for(int i=0;i<50;i++){
Card card = new Card("Card UI Example "+i, "Very Good");
data.add(card);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
運(yùn)行效果如下:
p>
當(dāng)然啦,Github上面也有專門的實(shí)現(xiàn)card的library,這里列舉兩個(gè)不錯(cuò)的library
cardslib:地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android編程實(shí)現(xiàn)圖片的瀏覽、縮放、拖動(dòng)和自動(dòng)居中效果
- Android實(shí)現(xiàn)圖片拖動(dòng)效果
- Android如何創(chuàng)建可拖動(dòng)的圖片控件
- Android通過自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
- Android RecyclerView多類型布局卡片解決方案
- Android控件CardView實(shí)現(xiàn)卡片布局
- Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法
- Android實(shí)現(xiàn)可拖動(dòng)層疊卡片布局
相關(guān)文章
Android客戶端post請(qǐng)求服務(wù)器端實(shí)例
這篇文章主要介紹了Android客戶端post請(qǐng)求服務(wù)器端實(shí)例,本文講解了Android客戶端與服務(wù)器端通信方式、解析服務(wù)器端返回?cái)?shù)據(jù)的解釋、用GET和POST訪問http資源等內(nèi)容,并給出了一個(gè)POST實(shí)例,需要的朋友可以參考下2015-06-06
Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式
這篇文章主要介紹了Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式 的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下)
這篇文章主要介紹了Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下) ,需要的朋友可以參考下2017-04-04
學(xué)習(xí)Android開發(fā)之RecyclerView使用初探
Android開發(fā)學(xué)習(xí)之路的第一課RecyclerView使用初探,感興趣的小伙伴們可以參考一下2016-07-07
Android 使用幀動(dòng)畫內(nèi)存溢出解決方案
這篇文章主要介紹了Android 使用幀動(dòng)畫內(nèi)存溢出解決方案的相關(guān)資料,這里提供了詳細(xì)的解決辦法,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
Android中顏色選擇器和改變字體顏色的實(shí)例教程
這篇文章主要介紹了Android中顏色選擇器和改變字體顏色的實(shí)例教程,其中改變字體顏色用到了ColorPicker顏色選擇器,需要的朋友可以參考下2016-04-04
android實(shí)現(xiàn)http中請(qǐng)求訪問添加cookie的方法
這篇文章主要介紹了android實(shí)現(xiàn)http中請(qǐng)求訪問添加cookie的方法,實(shí)例分析了兩種添加cookie的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
分享一個(gè)Android設(shè)置圓形圖片的特別方法
圓形圖片想必是項(xiàng)目開發(fā)中也是不少用的一個(gè)知識(shí)點(diǎn)吧。那么這里學(xué)習(xí)一下簡(jiǎn)單的制作圓形圖片,這個(gè)方法不用于平時(shí)的實(shí)現(xiàn)方法,有需要的可以參考借鑒。2016-09-09
Android SDK Manager解決更新時(shí)的問題 :Failed to fetch URL...
本文主要介紹解決安裝使用SDK Manager更新時(shí)的問題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問題辦法,有需要的小伙伴可以參考下2016-09-09
詳解Android中使用Notification實(shí)現(xiàn)進(jìn)度通知欄(示例三)
這篇文章主要介紹了詳解Android中使用Notification實(shí)現(xiàn)進(jìn)度通知欄(示例三),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12

