添加视图到的LinearLayout体重编程 [英] Adding Views to LinearLayout with weight programmatically

查看:135
本文介绍了添加视图到的LinearLayout体重编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被卡住以下问题:
对于一个问题的应用程序我想要实现一个漂亮的输出显示了可能的答案和人口比例的调查回答了具体的问题。因此,我想在酒吧添加到我的应用程序显示的百分比。我试图通过使用视图和线性布局加权他们解决这个问题。

I am getting stucked on the following problem: For a questions app I want to implement a nice output for a survey showing the possible answers and the percentage of people answered the specific question. Therefore, I want to add a "bar" to my app that displays the percentage. I try to solve this by using views and weighting them in a linear layout.

我要以编程方式添加了不同的答案,这是code到目前为止,我已经得到了。
我的问题是,我不是越来越接近加权的意见,并调整它们。

I want to add the different answers programmatically, this is the code I've got so far. My problems are, that I am not getting closer to weighting the views and resizing them.

/* Add all questions */ 
RelativeLayout my_root = (RelativeLayout) findViewById(R.id.ownerRL);

/* Add a new Linearlayout as a container for the question */
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(A);

/* Create a new View in this container, for the status bar */
View new_view = new View(getBaseContext());
new_view.setBackgroundColor(Color.YELLOW);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 20, 3);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) A.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.question1);   

A.addView(new_view);
View new_view2 = new View(getBaseContext());
new_view.setBackgroundColor(Color.GREEN);
ViewGroup.LayoutParams lp2 = new ViewGroup.LayoutParams(50, 20);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) A.getLayoutParams();
params2.addRule(RelativeLayout.BELOW, R.id.question1);  
params2.addRule(RelativeLayout.LEFT_OF, new_view.getId());

A.addView(new_view2);

绿视图实际上应右黄色的(这是不可见)。

The Green view should actually be right of the yellow one (which is not visible).

如何得到它的管理,使得它创建具有4像素的高度,红色和重量份的权重?红/白酒吧

How do get it managed such that it creates a red/white bar that has a height of 4px and the red and weight parts are weighted?

感谢您的帮助!

推荐答案

检查<一href=\"http://stackoverflow.com/questions/4641072/how-to-set-layout-weight-attribute-dynamically-from-$c$c\">this链接,组线性布局重像这样

check this link, Set Linear Layout weight it like this

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);

最后一个参数是weight.Hope这种帮助。

The last parameter is weight.Hope this help.

这篇关于添加视图到的LinearLayout体重编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆