編輯:關於 Android 程式設計
圓形頭像在現今的 App 裡隨處可見,視覺上確實比方形柔和。實作上有兩條路:一是在出圖階段就把素材裁成圓形,二是用自定義 View 在執行期自動把任意圖片裁圓。本文走第二條路,並搭配 CardView 做一個完整的列表畫面。
效果預覽:

圓形裁切這裡採用 GitHub 上的 CircleImageView,專案位址:https://github.com/hdodenhof/CircleImageView 。它除了裁圓,還能幫頭像加上描邊,很符合設計稿常見的需求。
CardView 顧名思義是卡片式容器,它繼承自 FrameLayout,因此內部控件的堆疊關係要特別留意。陰影、圓角等效果都可以透過屬性設定,是實作卡片列表的標準選擇。
新建專案時選擇 Navigation Drawer Activity,讓 Android Studio 自動產生初始骨架,接著動兩個檔案。
修改 nav_header_main,加入圓角頭像
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <de.hdodenhof.circleimageview.CircleImageView xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/profile_image" android:layout_width="96dp" android:layout_height="96dp" android:src="@drawable/darth_vader" app:civ_border_width="2dp" /> </LinearLayout>
這裡用的是官方版的完整套件路徑 de.hdodenhof.circleimageview.CircleImageView,描邊寬度透過 civ_border_width 設定。
修改 content_main,加入 RecyclerView(記得導包)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.xw.design2.MainActivity" tools:showIn="@layout/app_bar_main"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </RelativeLayout>
新增 item 佈局,使用 CardView(同樣別忘了導包)
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView android:layout_height="wrap_content" android:layout_width="match_parent" xmlns:card_view="https://schemas.android.com/apk/res-auto" card_view:contentPadding="10dp" card_view:cardBackgroundColor="#303069" card_view:cardCornerRadius="10dp" card_view:cardPreventCornerOverlap="true" card_view:cardUseCompatPadding="true" xmlns:android="https://schemas.android.com/apk/res/android"> <TextView android:id="@+id/tv" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </android.support.v7.widget.CardView>
幾個屬性值得說明:contentPadding 控制內容內縮距離,cardCornerRadius 決定圓角,cardBackgroundColor 設定卡片底色。cardUseCompatPadding 打開後,在低版本系統上也能呈現一致的內距表現,避免不同 API 版本視覺落差。
1. 宣告成員變數與資料來源
private RecyclerView mRecyclerView; private String[] data={"2014 年,隨著 Google 推出了全新的設計語言 Material Design,還迎來了新的 Android 支持庫 v7,其中就包含了 Material Design 設計語言中關於 Card 卡片概念的實現 —— CardView。" ,"經歷了相當長的一段時間相信許多 Android 開發者都已經應用了這個控件,現在才寫這篇文章可能有點晚,但對於剛剛開始使用的開發者以及其他已經使用了一段時間但做出來效果不好的同學應該能幫上點小忙。" ,"Google 在 Android Lollipop 中引入了 Material Design 設計中的陰影(Elevation)和 Z 軸位移,其目的就是突出界面中不同元素之間的層次關系" ,"明年夏天,自由球員布雷克-格裡芬可能重返俄克拉何馬城與拉塞爾-威斯布魯克聯手。如果實現,雷霆隊能真正意義上地威脅勇士隊嗎?"};
2. 建立 ViewHolder
class MyHolder extends RecyclerView.ViewHolder{ private TextView mTextView; public MyHolder(View itemView) { super(itemView); mTextView= (TextView) itemView.findViewById(R.id.tv); } }
3. 建立 Adapter
class MyAdapter extends RecyclerView.Adapter<MyHolder>{ @Override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater=LayoutInflater.from(getApplicationContext()); View v=layoutInflater.inflate(R.layout.item,parent,false); MyHolder holder=new MyHolder(v); return holder; } @Override public void onBindViewHolder(MyHolder holder, int position) { holder.mTextView.setText(data[position]); } @Override public int getItemCount() { return data.length; } }
4. 在 onCreate() 中設定 Adapter
mRecyclerView= (RecyclerView) findViewById(R.id.rv); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(new MyAdapter());
整個流程拆開來看其實很直觀:CircleImageView 負責頭像外觀,CardView 負責卡片容器,RecyclerView 負責列表滑動與回收。三者各司其職,之後要換頭像來源或改卡片樣式都不會互相牽動。以上就是本次分享的全部內容,若有疑問歡迎留言交流。
MPAndroidChart 圖表庫(十二):正負堆疊條形圖實作
接續上一篇。這次要畫的圖表與前一篇幾乎同構,只是數值有了正反兩面——一個人數分布圖,男性朝左、女性朝右。先看效果: 因為結構相似,程式碼也大同小異,直接看基本實作。 一
WebView 與 JavaScript 雙向互動:分享控制與登入狀態同步
專案裡遇到一個典型需求:H5 頁面需要與原生 App 互相呼叫。這篇用一個具體情境把整個流程走完。 需求拆解 1. 點按鈕進入 WebView 頁面;H5 決定要不要顯
橫豎屏切換時 Activity 生命週期怎麼走?五種 configChanges 配置實測
螢幕旋轉是 Android 開發中最容易被低估的坑之一:轉一次屏,Activity 可能被完整銷毀重建,也可能只安靜地走一個回呼,差別幾乎全取決於清單文件裡 androi
Android 仿微信朋友圈點讚與評論彈出框實作
微信朋友圈的點讚與評論互動,是許多社群型介面會參考的模式。本文以 ListView 為基礎,示範如何做出類似效果:點擊更多按鈕後彈出操作框,選擇評論後開啟輸入區,並把新的