直播平臺軟體開發,實現自定義標題欄

zhibo系統開發發表於2023-02-23

直播平臺軟體開發,實現自定義標題欄

新建一個class繼承一個相對佈局並重寫其構造方法

public class CustomTitleBlock extends RelativeLayout {
    public CustomTitleBlock(Context context) {
        super(context);
    }
    public CustomTitleBlock(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}


建立一個Layout檔案用於CustomTitleBlock.class繫結並初始化相關控制元件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
    xmlns:tools="
    xmlns:app="
    android:id="@+id/layout_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <ImageView
                android:id="@+id/iv_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <TextView
                android:id="@+id/tv_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/iv_left"
                app:layout_constraintTop_toTopOf="parent"  />
        </androidx.constraintlayout.widget.ConstraintLayout>
        <TextView
            android:id="@+id/tv_centent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"/>
        <EditText
            android:id="@+id/search_box"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:textSize="14sp"
            android:background="@drawable/search_box_style"
            android:drawableLeft="@drawable/search_64"
            android:drawablePadding="6dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:visibility="invisible"
            app:layout_constraintLeft_toRightOf="@+id/left"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/right"/>
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent">
            <TextView
                android:id="@+id/tv_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/iv_right"/>
            <ImageView
                android:id="@+id/iv_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>


在values資料夾下建立一個attrs.xml檔案用於宣告控制元件的屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomTitleBlock">
        <!-- 標題欄左邊 -->
        <attr name="leftText" format="string"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftTextSize" format="integer"/>
        <attr name="leftImage" format="reference"/>
        <!-- 標題欄中間 -->
        <attr name="centerText" format="string"/>
        <attr name="centerTextColor" format="color"/>
        <attr name="centerTextSize" format="integer"/>
        <!-- 搜尋框 -->
        <attr name="searchBox" format="boolean"/>
        <attr name="searchBoxHint" format="string"/>
        <attr name="searchBoxWidth" format="dimension">
            <enum name="match_parent" value="-1"/>
            <enum name="wrap_content" value="-2"/>
        </attr>
        <!-- 標題欄右邊 -->
        <attr name="rightText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightTextSize" format="integer"/>
        <attr name="rightImage" format="reference"/>
    </declare-styleable>
</resources>


 以上就是 直播平臺軟體開發,實現自定義標題欄,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2936635/,如需轉載,請註明出處,否則將追究法律責任。

相關文章