常用代码模板

1、BaseAbstractActivity

private fun configView() {
    val inflater = LayoutInflater.from(this)
    val baseView = inflater.inflate(getLayoutId(), null)
    setContentView(baseView)
}

2、动态设置布局

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout>
    <RelativeLayout>
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

java

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) relativeLayout.getLayoutParams();
params.width = (int) width; //px
params.height = (int) height;  //px
relativeLayout.setLayoutParams(params);
//dp即dip
/**
 * dp转换为px
 */
fun dp2x(context: Context, dipValue: Int): Int {
    val density: Float = context.resources.displayMetrics.density
    return (dipValue * density + 0.5f).toInt()
}

/**
 * px转换为dp
 */
fun px2dp(context: Context, pxValue: Int): Int {
    val density: Float = context.resources.displayMetrics.density
    return (pxValue / density + 0.5f).toInt()
}

3、自定义View

3个构造方法

class MyView : View {

    private var mContext: Context? = null


    constructor(context: Context?) : super(context) {
        mContext = context
        initView()
    }

    constructor(
        context: Context?,
        attributeSet: AttributeSet?
    ) : super(context, attributeSet) {
        mContext = context
        initView()
    }

    constructor(context: Context?, attributeSet: AttributeSet?, def: Int) : super(
        context,
        attributeSet,
        def
    ) {
        mContext = context
        initView()
    }


    private fun initView() {
        //加载布局
        LayoutInflater.from(mContext).inflate(R.layout.layout_my_view, this)
        //绑定控件
        val tvTitle = findViewById<TextView>(R.id.id_tv_title)
    }
}

1个构造方法

class MyView(
    var mContext: Context,
    var attrs: AttributeSet? = null,
    var defStyleAttr: Int = 0
) : LinearLayout(mContext, attrs, defStyleAttr) {

    init {
        initView()
    }

	private fun initView() {
        //加载布局
        LayoutInflater.from(mContext).inflate(R.layout.my_view, this)
        //绑定控件
        val tvTitle = findViewById<TextView>(R.id.id_tv_title)
    }
}

4、Dialog

class WaveChooseDialog(
    val mContext: Context?,
) : Dialog(mContext) {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.dialog_wave)

        window?.run {
            setGravity(Gravity.BOTTOM)
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        setCancelable(false)


        mImgClose = findViewById(R.id.id_img_close)
        mImgClose.setOnClickListener {
            dismiss()
        }
    }

}
  • 设置 Dialog 长宽比
/**
     * 设置 dialog的高宽
     * 在dialog.show()之后调用
     *
     * @param dlg
     * @param activity
     */
    public static void setDialogWindowAttr(Dialog dlg, Activity activity) {
        Window window = dlg.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        //设置高宽比
        float screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
        double width = screenWidth * 0.9;
//        double height = 131.0 / 175.0 * width;
        lp.width = (int) width;
//        lp.height = (int) height;
        dlg.getWindow().setAttributes(lp);
    }

5、动态加载View

View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_handover_success, null, false);
ViewDataBinding binding = DataBindingUtil.inflate(
        LayoutInflater.from(mContext),
        R.layout.dialog_handover_success,
        null,
        false
);
View view = binding.getRoot();

6、RecycerView

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.ryPackages.setAdapter(new LeonAdapter(getActivity(), list));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KillerNoBlood

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值