Linearlayout programmatically - How to set up a divider?

做~自己de王妃 提交于 2019-12-07 21:44:55

问题


I am creating TextViews in LinearLayout programmatically and I would like to separate them with a divider (just a simple line). I have googled endlessly, what I have found is that I can use .setDividerDrawable, but I don't want to use external images for this. Any tips?


回答1:


How to Add Divider to an Android Layout Programmatically

Create a View 1 or 2 pixels tall and width match_parent and set the background color to whatever color you want the divider to be.

Separate the divider from the items above and below with margin settings.

Example:

ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = 
    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);



回答2:


You could use a simple drawable in xml for the divider (example here), or use a 9-patch image which barely takes anything.

Then, use the LinearLayoutICS in order to show the divider on most of the devices. you can check out this post i've made about it.




回答3:


For linear layout you can use this attribute to set divider android:divider="some color" android:showDividers="middle"



来源:https://stackoverflow.com/questions/15622711/linearlayout-programmatically-how-to-set-up-a-divider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!