Is it possible to set xml layout as a view not from resources dynamically?

大城市里の小女人 提交于 2019-12-03 21:32:47

Try Proteus

Proteus is meant to be a drop-in replacement for Android’s LayoutInflater; but unlike the compiled XML layouts bundled in the APK, Proteus inflates layouts at runtime. With Proteus, you can control your Apps layout from the backend (no WebViews). Forget the boilerplate code to findViewById, cast it to a TextView, and then setText(). Proteus has runtime data bindings and formatters. Plugin in your own custom views and attributes and formatters.

p.s. large parts of Flipart is built using proteus

Butter knife used only for view binding.For this process you need fetch XML file from storage and then convert it into the view and then set as your layout.

No, you can't send layout file from the server and inflate it dynamically. All views in Android have IDs mapped in R.java file. You can't and shouldn't modify it manually.

Butterknife is used for View Binding and help to reduce findViewById boilerplate. You can't use Butterknife to add Views.

However, you can dynamically create views using Java code and add them to any container layout. eg You can add a button to a Linear layout like this.

ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayoutID);

Button someButton = new Button(this);
someButton .setText("A Button");
someButton .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                                    LayoutParams.WRAP_CONTENT));
linerLayout.addView(someButton );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!