android wear打开卡片,Android Wear 开发-创建第一个卡片

最近老板要做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项目,就会出现卡片。还可以给布局加个背景,会有更漂亮的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值