首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数据和布局是如何在proteus布局充气中传递的?

数据和布局是如何在proteus布局充气中传递的?
EN

Stack Overflow用户
提问于 2019-06-28 19:40:41
回答 2查看 233关注 0票数 1

我试图使简单的安卓应用程序,使用proteus插件创建动态视图从json文件。我面临的问题,在传递proteus inflator.if布局和数据谁可以帮助我如何将数据传递到proteus充气?

EN

回答 2

Stack Overflow用户

发布于 2019-06-28 19:55:20

布局和数据都作为JSON对象传递给proteus充气器。因此,如果您正在使用任何web服务,您必须接收布局和数据作为JSON对象,然后使用proteusLayoutInflater.inflate(<layout>, <data>)创建视图,然后将该视图添加到ViewGroup。

票数 0
EN

Stack Overflow用户

发布于 2019-07-02 23:37:39

ActivityonCreate中尝试以下代码

代码语言:javascript
运行
复制
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());

查看simple-proteus-demo

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56806285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档