在livecard中更改linearlayout的背景

时间:2015-03-10 11:03:33

标签: google-glass google-gdk

当我的真实卡片发生某种状态时,我试图改变线性视图的颜色。我在colors.xml中添加了我想要的颜色,并且我可以在实时卡片布局中设置它们。

但有没有办法从代码中设置背景?我看到没有.setbackground或类似的东西。

有没有办法做到这一点,还是我需要使用图像?

1 个答案:

答案 0 :(得分:1)

您可以尝试在xml中提供布局ID,然后:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mLiveCard == null) {
        mLiveCard = new LiveCard(this, LIVE_CARD_TAG);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.live_card);
        remoteViews.setInt(R.id.my_layout, "setBackgroundResource", android.R.color.holo_red_dark);            
        mLiveCard.setViews(remoteViews);

        // Display the options menu when the live card is tapped.
        Intent menuIntent = new Intent(this, LiveCardMenuActivity.class);
        mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
        mLiveCard.publish(PublishMode.REVEAL);
    } else {
        mLiveCard.navigate();
    }
    return START_STICKY;
}

的xml:

<LinearLayout
android:id="@+id/my_layout"
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"
android:padding="@dimen/card_margin"
tools:context=".LiveCardService">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</LinearLayout>

Full project on GitHub.

相关问题