最近老板要做android手表,我也是第一次接触android手表,一脸茫然,不知道从何下手,android wear API 看的让我更是茫然,原谅我的英语水平不好,在网上找了很多资料,看的更是困惑,只能慢慢尝试。
Context Stream
API 的开始就介绍了Context Stream,我认为它是android wear设计的基础。
Context Stream是一个卡片的垂直列表,每一个显示一个有用的或及时的信息。很像谷歌现在在安卓手机和平板电脑上的功能,用户可以在垂直方向上滑动,从卡到卡。只有一张卡显示在一个时间,和背景照片是用来提供额外的视觉信息。您的应用程序可以创建卡,并将它们注入到流时,他们最有可能是有用的。
这个用户界面模型可以确保用户不必启动许多不同的应用程序来检查更新,他们可以简单地浏览他们的流上的一个简短的更新什么对他们很重要。
既然谷歌推荐使用卡片,我就试着创建我的第一个卡片。
1.android studio 创建一个wear工程。
2。添加布局。我使用的是方形表盘
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="so.wih.android.jjewear.MainActivity"
tools:deviceIds="wear_square">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_square" />
android:layout_width="match_parent"
android:layout_height="match_parent">FrameLayout>
LinearLayout>
3.在代码中添加卡片。
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.wearable.view.CardFragment;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
//添加卡片
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
CardFragment mCardFragment = CardFragment.create(getString(R.string.cftitle), getString(R.string.cfdesc), R.mipmap.ic_launcher);
mFragmentTransaction.add(R.id.frame_layout,mCardFragment);
mFragmentTransaction.commit();
}
});
}
}
4.运行wear项目,就会出现卡片。还可以给布局加个背景,会有更漂亮的效果。