更新时间:2021-07-27 GMT+08:00
分享

自定义视图参考示例

自定义视图xml配置示例

自定义视图的xml配置(custom_view.xml)如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    <Button
        android:id="@+id/custom_button"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="点我消失">

    </Button>

</FrameLayout>

自定义视图code-demo

自定义视图的code-demo(CustomView.java)如下所示:

package com.hw.cloudgame.cloudgamesplatformb100.ui.customView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import com.hw.cloudgame.cloudgamesplatformb100.R;

public class CustomView extends FrameLayout {
    private static String TAG = "CustomView";
    private Context mContext = null;
    private Button mCustomButton;

    public CustomViewFrameLayout(Context context) {
        super(context);
        mContext = context;
        //设置背景透明
        setBackgroundColor(0x00000000);
        //控制view
        initView();
    }


    private void initView(){
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) layoutInflater.inflate(R.layout.custom_view, null, false);
        addView(viewGroup);
        mCustomButton = findViewById(R.id.custom_button);
        mCustomButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something
				mCustomButton.setVisibility(View.GONE);
            }
        });
    }
}
分享:

    相关文档

    相关产品

close