DataBinding With Android 对话框
DataBinding With Android Dialog
我已经在 Activity
、Fragment
和 RecyclerView
中实现了 DataBinding
。现在尝试在 Dialog
中执行此操作,但对如何在其中设置自定义视图有点困惑?
这是我为 Dialog
实现的代码。
Dialog dialog = new Dialog(context);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
LayoutTermsBinding termsBinding;
dialog.setContentView(R.layout.layout_terms);
dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
dialog.show();
我知道如果是 Activity
我们可以执行 DataBindingUtil.setContentView()
并且对于 Fragment
我们可以执行 DataBindingUtil.inflate()
但我对如何转换 dialog.setContentView(R.layout.layout_terms);
与 DataBinding
.
假设这是你的 layout_terms.xml
:
<layout>
<data>
<!--You don't even need to use this one, this is important/necessary for the inflate method -->
<variable name="testVariable" value="String" />
</data>
<LinearLayout>
<TextView />
</LinearLayout>
</layout>
首先,您需要Binding
。这是通过简单地对其充气来完成的:
/*
* This will only work, if you have a variable or something in your 'layout' tag,
* maybe build your project beforehand. Only then the inflate method can be found.
* context - the context you are in. The binding is my activities binding.
* You can get the root view somehow else.
*/
LayoutTermsBinding termsBinding = LayoutTermsBinding
.inflate(LayoutInflater.from(context), (ViewGroup) binding.getRoot(), false);
//without a variable this would be
LayoutTermsBinding termsBinding = DataBindingUtil.
inflate(LayoutInflater.from(context), R.layout.layout_terms, (ViewGroup) mainBinding.getRoot(), false);
第二步:将您的 termsBinding.getRoot()
设置为 ContentView
:
dialog.setContentView(termsBinding.getRoot());
大功告成。 :)
我已经在 Activity
、Fragment
和 RecyclerView
中实现了 DataBinding
。现在尝试在 Dialog
中执行此操作,但对如何在其中设置自定义视图有点困惑?
这是我为 Dialog
实现的代码。
Dialog dialog = new Dialog(context);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
LayoutTermsBinding termsBinding;
dialog.setContentView(R.layout.layout_terms);
dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
dialog.show();
我知道如果是 Activity
我们可以执行 DataBindingUtil.setContentView()
并且对于 Fragment
我们可以执行 DataBindingUtil.inflate()
但我对如何转换 dialog.setContentView(R.layout.layout_terms);
与 DataBinding
.
假设这是你的 layout_terms.xml
:
<layout>
<data>
<!--You don't even need to use this one, this is important/necessary for the inflate method -->
<variable name="testVariable" value="String" />
</data>
<LinearLayout>
<TextView />
</LinearLayout>
</layout>
首先,您需要Binding
。这是通过简单地对其充气来完成的:
/*
* This will only work, if you have a variable or something in your 'layout' tag,
* maybe build your project beforehand. Only then the inflate method can be found.
* context - the context you are in. The binding is my activities binding.
* You can get the root view somehow else.
*/
LayoutTermsBinding termsBinding = LayoutTermsBinding
.inflate(LayoutInflater.from(context), (ViewGroup) binding.getRoot(), false);
//without a variable this would be
LayoutTermsBinding termsBinding = DataBindingUtil.
inflate(LayoutInflater.from(context), R.layout.layout_terms, (ViewGroup) mainBinding.getRoot(), false);
第二步:将您的 termsBinding.getRoot()
设置为 ContentView
:
dialog.setContentView(termsBinding.getRoot());
大功告成。 :)