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));