我试图使简单的安卓应用程序,使用proteus插件创建动态视图从json文件。我面临的问题,在传递proteus inflator.if布局和数据谁可以帮助我如何将数据传递到proteus充气?
发布于 2019-06-28 19:55:20
布局和数据都作为JSON对象传递给proteus充气器。因此,如果您正在使用任何web服务,您必须接收布局和数据作为JSON对象,然后使用proteusLayoutInflater.inflate(<layout>, <data>)
创建视图,然后将该视图添加到ViewGroup。
发布于 2019-07-02 23:37:39
在Activity
的onCreate
中尝试以下代码
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // A FrameLayout
final String LAYOUT = "{\n" +
" \"type\": \"TextView\",\n" +
" \"textSize\": \"28sp\",\n" +
" \"text\": \"Hello World!\"\n" +
"}";
final String DATA = "{}";
ViewGroup container = findViewById(R.id.container); // container is the FrameLayout
// create a new instance of proteus
Proteus proteus = new ProteusBuilder().build();
// register proteus with a ProteusTypeAdapterFactory to deserialize proteus jsons
ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(this);
ProteusTypeAdapterFactory.PROTEUS_INSTANCE_HOLDER.setProteus(proteus);
// deserialize layout and data
Layout layout;
ObjectValue data;
try {
layout = adapter.LAYOUT_TYPE_ADAPTER.read(new JsonReader(new StringReader(LAYOUT)));
data = adapter.OBJECT_TYPE_ADAPTER.read(new JsonReader(new StringReader(DATA)));
} catch (IOException e) {
throw new RuntimeException(e);
}
// create a new ProteusLayoutInflater
ProteusContext context = proteus.createContextBuilder(this).build();
ProteusLayoutInflater inflater = context.getInflater();
// Inflate the layout
ProteusView view = inflater.inflate(layout, data, container, 0);
// Add the inflated layout into the container
container.addView(view.getAsView());
https://stackoverflow.com/questions/56806285
复制相似问题