赞
踩
视图绑定 | Android 开发者 | Android Developers
- android {
- ...
- viewBinding {
- enabled = true
- }
- }
如果您希望在生成绑定类时忽略某个布局文件,请将 tools:viewBindingIgnore="true"
属性添加到相应布局文件的根视图中:
- <LinearLayout
- ...
- tools:viewBindingIgnore="true" >
- ...
- </LinearLayout>
-
如需设置绑定类的实例以供 Activity 使用,请在 Activity 的 onCreate() 方法中执行以下步骤:
inflate()
方法。此操作会创建该绑定类的实例以供 Activity 使用。getRoot()
方法或使用 Kotlin 属性语法获取对根视图的引用。- private lateinit var binding: ResultProfileBinding
-
- override fun onCreate(savedInstanceState: Bundle) {
- super.onCreate(savedInstanceState)
- binding = ResultProfileBinding.inflate(layoutInflater)
- val view = binding.root
- setContentView(view)
- }
如需设置绑定类的实例以供 Fragment 使用,请在 Fragment 的 onCreateView() 方法中执行以下步骤:
inflate()
方法。此操作会创建该绑定类的实例以供 Fragment 使用。getRoot()
方法或使用 Kotlin 属性语法获取对根视图的引用。onCreateView()
方法返回根视图,使其成为屏幕上的活动视图。- private var _binding: ResultProfileBinding? = null
- private val binding get() = _binding!!
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = ResultProfileBinding.inflate(inflater, container, false)
- return binding.root
- }
-
- override fun onDestroyView() {
- super.onDestroyView()
- _binding = null
- }
- public class MyDialog extends Dialog {
-
- protected View mView;
- protected DialogBottomBinding mBinding;
-
- public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
- super(context, themeResId);
-
- //原来的写法
- // mView = View.inflate(getContext(), getLayoutId(), null);
-
- //使用ViewBinding的写法
- mBinding = DialogBottomBinding.inflate(getLayoutInflater());
- mView = mBinding.getRoot();
-
- setContentView(mView);
- }
- }
- // 自定义view
- public class MyLinearLayout extends LinearLayout {
- public MyLinearLayout(Context context) {
- this(context, null);
- }
-
- public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, 0);
- }
-
- public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
-
- // 正常添加布局(亲测有效)
- ViewMyLayoutBinding binding = LibPlateformLayoutBinding.inflate(LayoutInflater.from(getContext()), this, true);
-
- // 方法二:
- // val root = View.inflate(context, R.layout.widget_core, this)
- // binding = WidgetCoreBinding.bind(root)
-
- // 针对根标签为merge
- ViewMyLayoutMergeBinding binding = ViewMyLayoutMergeBinding.inflate(LayoutInflater.from(getContext()), this);
- }
-
- }
- class StudentAdapter(private val context: Context,
- private val list: List<AddressInfo>) : RecyclerView.Adapter<ItemViewHolder>() {
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
- val studentBinding = ItemAddressBinding.inflate(LayoutInflater.from(
- context), parent, false)
- return ItemViewHolder(studentBinding)
- }
-
- @SuppressLint("SetTextI18n")
- override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
- holder.itemBinding.tvName.text = "姓名:" + list[position].name
- }
-
- override fun getItemCount(): Int {
- return list.size
- }
-
- inner class ItemViewHolder(var itemBinding: ItemAddressBinding) : RecyclerView.ViewHolder(
- itemBinding.root)
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。