直播平台软件开发,实现自定义标题栏
直播平台软件开发,实现自定义标题栏
新建一个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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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>
以上就是 直播平台软件开发,实现自定义标题栏,更多内容欢迎关注之后的文章