MusicInfoView.java
package com.example.media.Component;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.example.media.R;
public class MusicInfoView extends LinearLayout {
private String musicTitle;
private String musicAuthor;
private TextView musicTitleView;
private TextView musicAuthorView;
public MusicInfoView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.music_info,this, true);
musicTitleView = (TextView) findViewById(R.id.music_title);
musicAuthorView = (TextView) findViewById(R.id.music_author);
// font set
AssetManager mgr = context.getAssets();
Typeface regularTf = Typeface.createFromAsset(mgr, "fonts/SourceHanSansCN-Regular.ttf");
Typeface robotoTf = Typeface.createFromAsset(mgr, "fonts/Roboto-Bold.ttf");
musicTitleView.setTypeface(robotoTf);
//musicAuthorView.setTypeface(regularTf);
}
// update music info
public void updateMusicInfo(String title, String author) {
if (title != null) {
musicTitleView.setText(title);
this.musicTitle = title;
} else {
musicTitleView.setText(musicTitle);
}
if (author != null) {
musicAuthorView.setText(author);
this.musicAuthor = author;
} else {
musicAuthorView.setText(musicAuthor);
}
}
}
music_info.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="480dp"
android:layout_height="136dp"
android:background="#000000"
tools:context=".Component.MusicInfoView">
<TextView
android:id="@+id/music_title"
android:layout_width="match_parent"
android:layout_height="76dp"
android:text="Life Goes On"
android:textColor="#ffffff"
android:textSize="60sp"
android:textFontWeight="700"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<TextView
android:id="@+id/music_author"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginTop="82dp"
android:layout_marginBottom="0dp"
android:text="陈奕迅"
android:textColor="#ffffff"
android:textSize="36sp"
android:textFontWeight="400"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
<com.example.media.Component.MusicInfoView
android:id="@+id/music_info"
android:layout_width="480dp"
android:layout_height="136dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>