*以下內(nèi)容翻譯自android wear的官方教程握牧,本人水平有限,如有錯(cuò)誤歡迎指出
home
以下正文
卡片為不同的app提供了統(tǒng)一的信息通知娩梨。本文將教你怎么給你的app創(chuàng)建卡片沿腰。
Wearable UI Library提供了手表上卡片通知的實(shí)現(xiàn)。她包含了 CardFrame
狈定,這個(gè)類(lèi)可以將控件(views)包裹在一個(gè)白色背景的卡片樣式當(dāng)中(圓潤(rùn)的邊角和漸暗的陰影)颂龙。CardFrame只能包含一個(gè)子控件,通常是一個(gè)包含了其他各種控件的layout manager纽什。
你有兩種方法添加一個(gè)卡片:
- 使用或繼承CardFragment
- 在你的layout當(dāng)中添加 CardScrollView
實(shí)例措嵌。
注意:本文將告訴你如何添加卡片,而在adroid wear上的通知也是以卡片的形式展示芦缰。 Adding Wearable Features to Notifications
創(chuàng)建一個(gè)卡片F(xiàn)ragment
CardFragment
提供了一個(gè)默認(rèn)的標(biāo)題企巢,內(nèi)容和一個(gè)小圖標(biāo)(icon)。
添加CardFragment的方法:
- 在你的layout當(dāng)中為包含這個(gè)card的元素指定一個(gè)id
- 在activity中創(chuàng)建CardFragment的實(shí)例
- 用fragment manager把這個(gè)實(shí)例加載到包裹她的容器(container)中
layout:
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/robot_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_box="bottom">
</FrameLayout>
</android.support.wearable.view.BoxInsetLayout>
activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_activity2);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
CardFragment cardFragment = CardFragment.create(getString(R.string.cftitle), getString(R.string.cfdesc),R.drawable.p);
fragmentTransaction.add(R.id.frame_layout, cardFragment);
fragmentTransaction.commit();
}
自定義卡片樣式可以繼承CardFragment让蕾,然后重寫(xiě)(override) [onCreateContentView
](http://developer.android.youdaxue.com/reference/android/support/wearable/view/CardFragment.html#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)) 這個(gè)方法
在你的layout當(dāng)中添加CardFrame
你也可以直接在你的layout當(dāng)中添加卡片浪规。當(dāng)你想要自定義卡片樣式的時(shí)候,可以使用這種方法探孝。
圖片效果的layout,你也可以創(chuàng)建更加復(fù)雜的布局:
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/robot_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.wearable.view.CardScrollView
android:id="@+id/card_scroll_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_box="bottom">
<android.support.wearable.view.CardFrame
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/custom_card"
android:textColor="@color/black"
android:textSize="20sp"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/description"
android:textColor="@color/black"
android:textSize="14sp"/>
</LinearLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
<CardScrollView>
元素探測(cè)屏幕的形狀笋婿,并在不同的屏幕上顯示不一樣的卡片。在圓屏上使用更寬的margins顿颅。無(wú)論如何缸濒,在 <BoxInsetLayout>
中的 <CardScrollView>
被設(shè)置了layout_box="bottom"屬性后,卡片就可以在底部對(duì)齊且內(nèi)容不會(huì)被剪切。