gpt4 book ai didi

android - 如何以编程方式固定 View 在线性布局中的位置

转载 作者:行者123 更新时间:2023-11-29 02:06:02 25 4
gpt4 key购买 nike

我在这里以编程方式将 LinearLayout(child view) 添加到另一个 LinearLayout(parentview) 我想将 subview 的位置设置为 center_horizo​​ntal。怎么做?请有人帮助我。

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);

最佳答案

使用这个小改动:::

  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);

关于android - 如何以编程方式固定 View 在线性布局中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9977152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com