public class HomePageTitleLayout extends RelativeLayout {
private TextView tv_title;
private TextView tv_new;
private RelativeLayout ral_more;
private OnCliclkToMoreListenter onCliclkToMoreListenter;
private boolean isShowNew = false; //是否显示图标
private String title = ""; //标题内容
public HomePageTitleLayout(Context context) {
this(context, null);
}
public HomePageTitleLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HomePageTitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs, defStyleAttr);
}
private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.item_home_page_title_container, this, true);
tv_title = view.findViewById(R.id.tv_title);
tv_new = view.findViewById(R.id.tv_new);
ral_more = view.findViewById(R.id.ral_more);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HomePageTitleLayout);
title = typedArray.getString(R.styleable.HomePageTitleLayout_title);
isShowNew = typedArray.getBoolean(R.styleable.HomePageTitleLayout_showNew, false);
if (!"".equals(title)) {
tv_title.setText(title);
}
if (null != tv_new) {
tv_new.setVisibility(isShowNew ? VISIBLE : GONE);
}
if (null != ral_more) { //加载更多
ral_more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != onCliclkToMoreListenter) {
onCliclkToMoreListenter.onClickToMore(v);
}
}
});
}
typedArray.recycle();
}
public void setOnCliclkToMoreListenter(OnCliclkToMoreListenter listenter) {
this.onCliclkToMoreListenter = listenter;
}
private interface OnCliclkToMoreListenter {
void onClickToMore(View view);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现好货"
android:textColor="#ff333333"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_toRightOf="@id/tv_title"
android:background="@drawable/home_page_fx_new_bg"
android:paddingBottom="4dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="4dp"
android:text="NEW"
android:textColor="#ffffffff"
android:textSize="9sp" />
<RelativeLayout
android:id="@+id/ral_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="9dp"
android:text="更多"
android:textColor="#ff333333"
android:textSize="13sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/common_rig_arrow_icon" />
</RelativeLayout>
</RelativeLayout>
R.styleable.HomePageTitleLayout
<declare-styleable name="HomePageTitleLayout">
<attr name="title" format="string"></attr>
<attr name="showNew" format="boolean"></attr>
</declare-styleable>
示例:
<com.syt.zyyqg.widget.view.HomePageTitleLayout
android:id="@+id/home_page_fx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="9dp"
android:layout_marginTop="14dp"
app:showNew="true"
app:title="发现好货" />

本文介绍了一个自定义的Android布局类HomePageTitleLayout,该布局包括标题、新标签和加载更多的功能,并通过属性设置其显示状态。文章详细展示了如何在XML中使用这个自定义布局,并通过属性来定制其外观。

275

被折叠的 条评论
为什么被折叠?



