我正在以编程方式将LinearLayout(子视图)添加到另一个LinearLayout(parentview)这里我想将子视图的位置设置为center_horizontal。怎么做?请有人帮助我。
code
LinearLayout linearLayoutstate = new LinearLayout(this);
linearLayoutstate.setOrientation(LinearLayout.HORIZONTAL);
TextView stateTitletv = new TextView(this);
stateTitletv.setText("tv1");
TextView state_valuetv = new TextView(this);
state_valuetv.setText("tv2");
linearLayoutstate.addView(stateTitletv);
linearLayoutstate.addView(state_valuetv);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL);
LL_SelectedFilters.addView(linearLayoutstate,layoutParams);
答案 0 :(得分:1)
使用这个小改动:::
LinearLayout linearLayoutstate = new LinearLayout(this);
linearLayoutstate.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutstate.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
TextView stateTitletv = new TextView(this);
stateTitletv.setText("tv1");
TextView state_valuetv = new TextView(this);
state_valuetv.setText("tv2");
linearLayoutstate.addView(stateTitletv);
linearLayoutstate.addView(state_valuetv);
LL_SelectedFilters.addView(linearLayoutstate,layoutParams);
答案 1 :(得分:0)
问题的关键在于您孩子视图的gravity
的{{1}}字段:
LinearLayout.LayoutParams