Generally, Sri Lanka is one of the safest countries in Asia to travel solo as a female, however unwanted attention can still be an issue.
Women traveling alone may experience uncomfortable levels of male attention. Outside Colombo, it's a good idea to cover your legs and shoulders, though you’ll be stared at no matter what you wear. Tight tops are a bad idea. And away from the popular tourist beaches of the South, East and West, consider swimming in a T-shirt and shorts.
In Colombo and popular tourist areas you can relax the dress code. "Are you married?" could be the snappy conversation starter you hear most often, so if you are single consider wearing a fake wedding ring and carrying a few pics of your imaginary partner back home. It's better to avoid personal questions all together. Listen to music or look busy on your phone if firmly (but politely) saying you're not interested is tough for you.
Women traveling alone may be hassled while walking around day and night, or while exploring isolated places. Physical harassment (grabbing and groping) may occur anywhere. Single women may be followed, so try to be connected with larger groups of people. There have also been cases of solo women being attacked by guides at heritage sites; again, don’t go alone.
Women travelling solo will find buses and trains trying at times. In Colombo ordinary buses are so packed that sometimes it’s impossible to avoid bodily contact with other passengers. Stray hands on crowded buses and trains happen; this is something that local women are also subjected to. Change your seat or sit with a local woman. If you gesture to a local woman to sit next to you, she’ll understand. Women travelling solo should avoid night trains.
If you're a solo female travelers in Sri Lanka, don’t take unnecessary risks and trust your gut. If you feel unsafe where you are, walk away from the situation asap in a calm, inconspicuous yet brisk manner. If it means paying a little more money for a taxi on said occasion, do it. Never compromise your safety.
Book your accommodation in advance, don't take offers from locals upon arrival!
In the end, traveling in Sri Lanka is not one long hassle. Unpleasant incidents are the exception, not the rule. But remember there are many social environments that are almost exclusively male in character – local bars, for example. It's better to avoid them if you're alone.