layout_height = “50dp”,
text = “click”
},
}
local view = loadlayout(layout)
return view
end
运行一下,
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0QCbixsu-1651146671758)(https://user-gold-cdn.xitu.io/2018/11/18/167261767ddce265?imageslim)]
屏幕中上半部分是 Android 的 xml 布局中写好的代码,当点击运行按钮时,加载 lua 脚本,返回一个 View 对象,然后添加到布局中。一个简单的 lua 脚本编写的视图就写好了。 接下来修改一下,设置个点击事件。
require “import”
import “android.widget."
import "android.content.”
function getView()
local layout = {
LinearLayout,
orientation = “vertical”,
layout_width = “fill”,
layout_height = “fill”,
{
Button,
id = " 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 btn",
layout_marginTop=“8dp”,
layout_width = “fill”,
layout_height = “50dp”,
text = “click”
},
}
local ids = {} – store ids to find view
local view = loadlayout(layout, ids)
ids.btn.onClick = function()
Toast.makeText(activity,“2333”,0).show()
end
return view
end
运行效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-abqkFbKo-1651146671759)(https://user-gold-cdn.xitu.io/2018/11/18/1672617fce0cf29f?imageslim)]
再来个稍微复杂点的例子,写个列表,新建 list.lua
文件,放在手机的 sdcard/list.lua
require “import”
import “android.widget."
import "android.content.”
import “android.view.View”
import “androlua.LuaHttp”
import “androlua.LuaAdapter”
import “androlua.LuaImageLoader”
local JSON = require(“cjson”)
local uihelper = require(‘uihelper’)
– create view table
local layout = {
LinearLayout,
orientation = “vertical”,
layout_width = “fill”,
layout_height = “fill”,
{
ListView,
id = “listview”,
dividerHeight = 0,
layout_width = “fill”,
layout_height = “fill”,
},
}
local item_view = {
FrameLayout,
layout_width = “fill”,
layout_height = “240dp”,
{
ImageView,
id = “iv_image”,
layout_width = “fill”,
layout_height = “fill”,
scaleType = “centerCrop”,
},
{
TextView,
id = “tv_title”,
background = “#66000000”,
layout_width = “fill”,
layout_height = “fill”,
padding = “32dp”,
gravity = “center”,
maxLines = “5”,
lineSpacingMultiplier = ‘1.2’,
textSize = “14sp”,
textColor = “#CCFFFFFF”,
},
}
local data = {
dailyList = {}
}
local adapter
local function getData()
– http://baobab.kaiyanapp.com/api/v1/feed
local url = data.nextPageUrl
if url == nil then url = ‘http://baobab.kaiyanapp.com/api/v1/feed?udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04’ end
if url:find(‘udid=’) == nil then url = url … ‘&udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04’ end
print(url)
LuaHttp.request({ url = url }, function(error, code, body)
if error or code ~= 200 then
print(‘fetch data error’)
return
end
local str = JSON.decode(body)
uihelper.runOnUiThread(activity, function()
data.nextPageUrl = str.nextPageUrl
local list = str.dailyList[1].videoList
for i = 1, #list do
data.dailyList[#data.dailyList + 1] = list[i]
end
adapter.notifyDataSetChanged()
end)
end)
end
local function launchDetail(item)
Toast.makeText(activity, item.title, 0).show()
end
function getView()
local view = loadlayout(layout)
adapter = LuaAdapter(luajava.createProxy(“androlua.LuaAdapterKaTeX parse error: Expected '}', got '#' at position 49: …ction() return #̲data.dailyList …AdapterCreator”, {
getCount = function() return #data.dailyList end,