1

用于自定义通知的 Android Wear 应用程序。单击按钮会通知。但它只在卡片框架内显示黑屏,也无法通过从左到右滑动来关闭。我需要添加任何其他代码来制作卡片吗解雇。

主要活动

   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) {
            speakButton= (Button) findViewById(R.id.speakButton);
            speakButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                 public void onClick(View v) {
                    postNotif();
                 }
            });
        }
    });
}


private void postNotif(){
int notificationId=001;
Intent notificationIntent = new Intent(this, NotificationActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder =
        new Notification.Builder(this)
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .extend(new Notification.WearableExtender()
                        .setStartScrollBottom(false)
                        .setHintHideIcon(true)
                        .setDisplayIntent(notificationPendingIntent));
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notificationId,notificationBuilder.build());

}

NotificationActivity.class

public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result_card);
    }
}

search_result_card.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.wearable.view.BoxInsetLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:background="#00ffffff"
        android:id="@+id/fragment_container"
        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">
                <android.support.wearable.view.CardFrame
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="bottom"
                android:id="@+id/card_frame"
                android:alpha=".9">
                      <TextView
                        android:layout_height="match_parent"
                        android:layout_width="fill_parent"
                        android:text="Bnp Paribas"
                        android:textColor="@color/black"
                        android:textSize="18sp"
                        android:id="@+id/security_label"
                        android:alpha=".7" />
                           <TextView
                            android:fontFamily="sans-serif-light"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:layout_width="wrap_content"
                            android:text="45.99"
                            android:id="@+id/security_rate"
                            android:textColor="@color/black"
                            android:textSize="16sp"/>
                     <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal">
                           <TextView
                            android:fontFamily="sans-serif-light"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:text="▲ +1,21%"
                            android:textColor="@color/black"
                            android:layout_gravity="left|bottom"
                            android:textSize="14sp"
                            android:id="@+id/performance_percent"/>
                          <TextView
                            android:fontFamily="sans-serif-light"
                            android:layout_height="wrap_content"
                            android:layout_width="56dp"
                            android:text="+1,76 €"
                            android:id="@+id/perf_value"
                            android:textColor="@color/black"
                            android:layout_marginLeft="15dp"
                            android:textSize="14sp"
                            android:layout_gravity="right|bottom" />
                    </LinearLayout>
                </android.support.wearable.view.CardFrame>
        </android.support.wearable.view.CardScrollView>
    </android.support.wearable.view.BoxInsetLayout>

AndroidManifest.xml

<activity android:name=".NotificationActivity"
        android:exported="true"
        android:allowEmbedded="true"
        android:taskAffinity=""
        android:theme="@android:style/Theme.DeviceDefault.Light" />
4

0 回答 0