Android自定义LinearLayout引入xml布局,设置资源字体

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"
    />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于大博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值