问题
I want to set dynamic layout which is not present in resource like res or values.If it is present in my file-manager and i want to set that layout as a view layout.Is it possible to set dynamic layout to the views, without building app.I want to send the layout from the server and download the layout and set for the view without building and not from the resources.Is Butter knife used for this process or it's just for binding views to the variables.
回答1:
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
回答2:
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.
回答3:
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 );
来源:https://stackoverflow.com/questions/42366353/is-it-possible-to-set-xml-layout-as-a-view-not-from-resources-dynamically