[英]Android draw stripes of different height in a view
I'm using Java, Android studio.我正在使用 Java、Android 工作室。 In a view, I need to programmatically create a random number of stripes of different colours and height as a % of the parent view height on the screen.
在视图中,我需要以编程方式创建随机数量的不同颜色和高度的条纹,作为屏幕上父视图高度的百分比。 No charts.
没有图表。 Only a single column of stacked striped with varying heights.
只有一列堆叠的不同高度的条纹。 How can I achieve this?
我怎样才能做到这一点? This is as complex as it will ever get, no complicated libraries required.
这是最复杂的,不需要复杂的库。
Image: Stacked horizontal stripes of varying colour and height图片:不同颜色和高度的堆叠水平条纹
I spent 6 hours searching for a solution already, please help.我已经花了 6 个小时寻找解决方案,请帮忙。
If you do not want to give yourself a hard time, You'd rather use a layout (ViewGroup) instead of a View.如果您不想给自己带来困难,您宁愿使用布局(ViewGroup)而不是视图。
All you have to do is get yourself a randomizer (PRNG) and get yourself some colors.您所要做的就是给自己一个随机发生器 (PRNG) 并给自己一些 colors。
After that you can start inflating the layout with some views.之后,您可以开始使用一些视图来扩展布局。
final LinearLayout layout=(LinearLayout)findViewByid(R.id.YOUR_LINEARLAYOUT);
layout.setOrientation(LinearLayout.VERTICAL);
final Random rand=new Random();
for(int i=0;i<3;i++){
final LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
p.weight=(float)Math.random();
final TextView view=new TextView(this); //No listeners here!
view.setLayoutParams(p);
layout.addView(view);
view.setBackground(new ColorDrawable(
Color.rgb(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))
));
}
final LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(-1, 0);
final TextView view=new TextView(this);
view.setLayoutParams(p);
layout.addView(view);
view.setBackground(new ColorDrawable(
Color.rgb(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.