自定义组合布局

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值