View Binding 作为 Android Jetpack 的一部分发布
文档:https : //developer.android.com/topic/libraries/view-binding
我的问题是,如何将视图绑定与自定义视图一起使用。Google 文档仅展示了 Activity 和 Fragment。
我试过这个,但没有显示任何内容。
LayoutInflater inflater = LayoutInflater.from(getContext());
Run Code Online (Sandbox Code Playgroud)
然后,我使用了这个,但同样没有运气。
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我想也许我没有针对我的观点选择正确的布局充气器,但不确定。
我正在尝试将动态内容添加到使用XML创建的视图中.
我有一个视图"profile_list",它有一个我想添加一些元素的ScrollView.
这是我正在尝试做的一些代码.
// Find the ScrollView
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(R.layout.profile_list);
Run Code Online (Sandbox Code Playgroud)
计划是添加一个TableLayout并动态填充它,而不仅仅是一个虚拟文本,但首先我必须让它工作.
欢迎任何帮助.
亲切的问候Olle
愚蠢的我在我的XML文件中的ScrollView中留下了一个元素!
无论如何,她是一个有效的例子:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.profile_list, null);
// Find the ScrollView
ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = …Run Code Online (Sandbox Code Playgroud) 我尝试在 ConstraintLayout 中另一个视图的同一位置添加一个视图,但添加的视图没有获得另一个视图的 LayoutParams。
添加的视图发生在容器的左上角|。
这是我的代码:
TextView cloneView = new TextView(getContext());
cloneView.setLayoutParams(otherView.getLayoutParams());
mainContainer.addView(cloneView);
Run Code Online (Sandbox Code Playgroud) android ×3