2
我對以下問題感到困惑: 對於問題應用程序,我希望爲調查顯示可能的答案和答案的百分比提供很好的輸出。因此,我想爲顯示百分比的應用添加一個「欄」。我嘗試通過使用視圖並以線性佈局加權來解決此問題。以編程方式將視圖添加到LinearLayout中使用權重
我想以編程方式添加不同的答案,這是迄今爲止我得到的代碼。 我的問題是,我沒有越來越接近加權意見和調整他們的大小。
/* 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);
綠色視圖實際上應右黃色的(這是不可見)。
如何管理它以創建一個高度爲4px的紅色/白色條,紅色和重量部分是加權的?
感謝您的幫助!