安卓作業----慕課移動應用開發作業19之自定義Notification佈局

本篇運用ImageView和TextView等對Notification進行自定義佈局

同時這也是中國大學慕課移動終端應用開發的網課作業19,我會持續更新我的作業,如果有需要關注一下吧

說明

1.參考文章安卓仿網易雲音樂通知欄控制音樂
2.本篇只是自定義佈局,由於時間等問題,並沒有添加功能進去,如有興趣,請戳第一點鏈接。
3.感謝人美聲甜的顧同學的幫助,提供了運行截圖

運行截圖

在這裏插入圖片描述

代碼部分

1.MainActivity.java
public class MainActivity extends AppCompatActivity {
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = findViewById(R.id.open);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"被點擊",Toast.LENGTH_SHORT).show();

                RemoteViews view = new RemoteViews(getPackageName(), R.layout.my_notification_layout);

                Notification notification = new Notification.Builder(MainActivity.this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContent(view)//設置普通notification視圖
                        .build();

                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.notify(10, notification);

            }
        });


    }
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
    android:id="@+id/open"
    android:text="發送通知"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>
3. 自定義通知佈局 my_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="100dp">

    <ImageView
        android:src="@drawable/pic1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:orientation="vertical">
            <TextView
                android:layout_gravity="center"
                android:textSize="15dp"
                android:textColor="#333"
                android:layout_marginTop="10dp"
                android:text="江蘇民歌-----茉莉花"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="2"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3">
                <ImageView
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="20dp"
                    android:src="@drawable/icon2"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_centerVertical="true"/>
                <ImageView
                    android:layout_centerHorizontal="true"
                    android:src="@drawable/icon3"
                    android:layout_width="55dp"
                    android:layout_height="55dp"/>
                <ImageView
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/icon4"
                    android:layout_width="40dp"
                    android:layout_height="40dp"/>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ImageView
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:src="@drawable/icon1"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="5dp"/>
            <ImageView
                android:layout_gravity="center"
                android:src="@drawable/icon5"
                android:layout_width="40dp"
                android:layout_height="40dp"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

圖片資源

圖片資源來自阿里巴巴矢量圖標庫

總結

如果有什麼問題,請私信聯繫我或者在評論區留言
碼字不易,若有幫助,給個關注和讚唄

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章