美文网首页
AndroLua实现微信聊天界面布局

AndroLua实现微信聊天界面布局

作者: Ktry | 来源:发表于2020-02-28 00:23 被阅读0次

什么是AndroLua

截图如下
AndroLua仿微信聊天界面.jpg

实现代码如下

mess=
{
  LinearLayout;
  layout_height="wrap";
  layout_width="fill";
  orientation="horizontal";
  background="#FFEDEDED";
  {
    FrameLayout;
    layout_height="60dp";
    layout_width="50dp";
    {
      CardView;
      layout_height="fill";
      layout_width="fill";
      layout_marginTop="10dp";
      layout_marginLeft="10dp";
      layout_marginBottom="10dp";
      radius="4dp";
      elevation="0dp";
      CardBackgroundColor="#00DEDFE3";
      {
        ImageView;
        id="custom_head";
        layout_height="fill";
        layout_width="fill";
        scaleType="fitXY";
        id="youImg";
        src="/storage/emulated/0/tencent/MicroMsg/WeiXin/hdImg_ec3094e88c4b3cc5bc0199e5344dfe911580708012269.jpg";
        Visibility=View.INVISIBLE
      }
    }
  },
  {
    ImageView;
    id="custom_head";
    layout_marginTop='20dp';--布局顶距
    layout_height="20dp";
    layout_width="20dp";
    layout_marginRight="-10dp";
    scaleType="fitXY";
    src="/storage/emulated/0/Download/Jw3mAAAAAASUVORK5CYII=.png";
    id="youJt",
    --   Visibility=View.INVISIBLE
  },
  {
    LinearLayout;
    layout_height="wrap";
    layout_width="wrap";
    layout_marginTop="10dp";
    layout_weight="1";
    --  gravity='right';
    id="bbk";
    {
      CardView;
      layout_height="wrap";
      layout_width="wrap";
      layout_marginBottom="10dp";
      radius="4dp";
      elevation="0dp";
      CardBackgroundColor="#FFffffff";
      {
        TextView;--文本控件
        layout_width='wrap';--文本宽度
        layout_margin='10dp';--布局顶距
        textColor='#ff000000';--文字颜色
        text='测试第';--显示的文字
        gravity='center';
        textSize='15dp';--文字大小
        id="bodyT";
        Visibility=View.GONE
      };
      {
        ImageView;
        id="custom_head";
        layout_width="50%w";
        layout_height="50%w";
        scaleType="fitXY";
        id="bodyI";
        src="http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png";
        Visibility=View.GONE
      },
    }
  },
  {
    ImageView;
    id="custom_head";
    layout_marginTop='20dp';--布局顶距
    layout_height="20dp";
    layout_width="20dp";
    layout_marginLeft="-10dp";
    scaleType="fitXY";
    src="/storage/emulated/0/Download/Jw3mAAAAAASUVORK5CYII=.png";
    id="myJt",
    Visibility=View.INVISIBLE
  },
  {
    FrameLayout;
    layout_height="60dp";
    layout_width="50dp";
    {
      CardView;
      layout_height="fill";
      layout_width="fill";
      layout_marginTop="10dp";
      layout_marginRight="10dp";
      layout_marginBottom="10dp";
      radius="4dp";
      elevation="0dp";
      CardBackgroundColor="#00DEDFE3";
      {
        ImageView;
        id="custom_head";
        layout_height="fill";
        layout_width="fill";
        scaleType="fitXY";
        src="/storage/emulated/0/tencent/MicroMsg/WeiXin/hdImg_ec3094e88c4b3cc5bc0199e5344dfe911580708012269.jpg";
        id="myImg";
        Visibility=View.INVISIBLE
      }
    }
  },
}



SorrowClover=
{
  LinearLayout;--线性布局
  Orientation='vertical';--布局方向
  layout_width='fill';--布局宽度
  layout_height='fill';--布局高度
  background='#ffffff';--布局背景颜色(或图片路径)
  {
    ListView;--列表视图
    layout_width='fill';--布局宽度
    layout_height='fill';--布局高度
    DividerHeight='0';--设置分隔线宽度,0表示无分隔
    id="list";--设置id
  };
};
activity.setContentView(loadlayout(SorrowClover))--显示布局

data={}
adp=LuaAdapter(activity,data,mess)

myid="1"

JSON=import "json"
json_str=[==[
[
    {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "2"
,
        "userName": "桑榆"
,
        "bodyText": "这是一个消息"
,
        "time":"2020-0202 15:52:12",
        "bodyImg": ""
    },
  {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "1"
,
        "userName": "桑榆"
,
        "bodyText": "这是一个消息"
,
        "time":"2020-0202 15:52:12",
        "bodyImg": ""
    },
    {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "1"
,
        "userName": "Ktry"
,
        "bodyText": ""
,
        "time":"2020-0202 15:52:12",
        "bodyImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png"
    },
  {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "1"
,
        "userName": "桑榆"
,
        "bodyText": "这是一个消息"
,
        "time":"2020-0202 15:52:12",
        "bodyImg": ""
    },
    {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "2"
,
        "userName": "Ktry"
,
        "bodyText": ""
,
        "time":"2020-0202 15:52:12",
        "bodyImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png"
    },
    {
        "userImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png",
        "userId": "2"
,
        "userName": "Ktry"
,
        "bodyText": ""
,
        "time":"2020-0202 15:52:12",
        "bodyImg": "http://imgurl.xiaok1.cn/imgs/2019/09/33249270dff24754.png"
    }
]
]==]

json_o=JSON.decode(json_str)


function bodyGra(v)
  if(v==myid)then
    return{
      gravity=Gravity.RIGHT
    }
   else
    return{
      gravity=Gravity.LEFT
    }
  end
end

function userImgGra1(v,iv)
  if(iv==myid)then
    return{
      Visibility=View.INVISIBLE
    }
   else
    return{--头像
      src=v.userImg,
      onClick=function()
        print(v.userId)
      end,
      onLongClick=function()
        print(v.userName)
        return true
      end,
      Visibility=View.VISIBLE
    }
  end
end

function userImgGra2(v,iv)
  if(iv==myid)then
    return{--头像
      src=v.userImg,
      onClick=function()
        print(v.userId)
      end,
      onLongClick=function()
        print(v.userName)
        return true
      end,
      Visibility=View.VISIBLE
    }
   else

    return{
      Visibility=View.INVISIBLE
    }
  end
end

function bodyTb(v,vim)
  if(vim==nil or vim=="")then
    return{
      Visibility=View.GONE
    }
   else
    return{
      text=v.bodyText,
      onClick=function()
        print(v.time)
      end,
      Visibility=View.VISIBLE
    }
  end
end
function bodyIb(v,vim)
  if(vim==nil or vim=="")then
    return{
      Visibility=View.GONE
    }
   else
    return{
      src=v.bodyImg,
      onClick=function()
        print(v.time)
      end,
      Visibility=View.VISIBLE
    }
  end
end

function myJt1(i,vim)
  if(i~=myid or vim==nil or vim=="")then
    return{
      Visibility=View.INVISIBLE
    }
   else
    return{
      Visibility=View.VISIBLE
    }
  end
end

function youJt1(i,vim)
  if(i==myid or vim==nil or vim=="")then
    return{
      Visibility=View.INVISIBLE
    }
   else
    return{
      Visibility=View.VISIBLE
    }
  end
end

for i,v in ipairs(json_o) do


  table.insert(data,{
    bbk=bodyGra(v.userId),
    youImg=userImgGra1(v,v.userId),
    bodyT=bodyTb(v,v.bodyText),
    bodyI=bodyIb(v,v.bodyImg),
    myImg=userImgGra2(v,v.userId),
    myJt=myJt1(v.userId,v.bodyText),
    youJt=youJt1(v.userId,v.bodyText),
  })
end
--为list设置适配器
list.Adapter=adp

相关文章

网友评论

      本文标题:AndroLua实现微信聊天界面布局

      本文链接:https://www.haomeiwen.com/subject/nfexhhtx.html