添加的LinearLayout编程方式在Android不工作 [英] Adding LinearLayout programmatically in Android doesn't work
本文介绍了添加的LinearLayout编程方式在Android不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的层次结构是这样的:
- RelativeLayout的
- 的LinearLayout(垂直)
- 的FrameLayout(权重5)
- 的ImageView
- 查看(权重为1)
- 的FrameLayout(权重5)
- 的LinearLayout(垂直)
视图仅仅是用于间隔目的的虚拟视图。 我这样做是在布局XML和它的作品。 但是,当我想以编程方式做到这一点,下面的codeS不工作。
的LinearLayout LL =新的LinearLayout(本);
ImageView的阶梯=新ImageView的(这一点);
的FrameLayout ladderFL =新的FrameLayout(本);
查看dummyView =新景(本);
ladder.setImageResource(R.drawable.ladder1);
LL.setOrientation(LinearLayout.VERTICAL);
的LayoutParams LLParams =新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams ladderFLParams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,0);
ladderFLParams.weight = 5F;
LinearLayout.LayoutParams dummyParams =新LinearLayout.LayoutParams(0,0);
dummyParams.weight = 1F;
FrameLayout.LayoutParams ladderParams =新FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
ladder.setLayoutParams(ladderParams);
ladderFL.setLayoutParams(ladderFLParams);
dummyView.setLayoutParams(dummyParams);
LL.setWeightSum(1207米);
LL.setLayoutParams(LLParams);
ladderFL.addView(梯);
LL.addView(ladderFL);
LL.addView(dummyView);
((RelativeLayout的)findViewById(R.id.screenRL))addView(LL)。
解决方案
的LinearLayout LL =新的LinearLayout(本);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.VERTICAL);
的LayoutParams LLParams =新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LL.setWeightSum(1207米);
LL.setLayoutParams(LLParams);
ImageView的阶梯=新ImageView的(这一点);
ladder.setImageResource(R.drawable.ic_launcher);
FrameLayout.LayoutParams ladderParams =新FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
ladder.setLayoutParams(ladderParams);
的FrameLayout ladderFL =新的FrameLayout(本);
LinearLayout.LayoutParams ladderFLParams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,0);
ladderFLParams.weight = 5F;
ladderFL.setLayoutParams(ladderFLParams);
ladderFL.setBackgroundColor(Color.GREEN);
查看dummyView =新景(本);
LinearLayout.LayoutParams dummyParams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
dummyParams.weight = 1F;
dummyView.setLayoutParams(dummyParams);
dummyView.setBackgroundColor(Color.RED);
ladderFL.addView(梯);
LL.addView(ladderFL);
LL.addView(dummyView);
RelativeLayout的RL =((RelativeLayout的)findViewById(R.id.screenRL));
rl.addView(LL);
我已经安排你的code为了更好地理解,也给了它一个背景色,以获得清晰的图像,因为我不知道你想要的,你可以通过它的东西。我希望这是有帮助的。您应提供使用XML,所以我们知道你想要什么。
The hierarchy is like this:
- RelativeLayout
- LinearLayout (vertical)
- FrameLayout (weight 5)
- ImageView
- View (weight 1)
- FrameLayout (weight 5)
- LinearLayout (vertical)
The View is just a dummy view for spacing purpose. I did it in the layout xml and it works. But when I want to do it programmatically, the following codes do not work.
LinearLayout LL = new LinearLayout(this);
ImageView ladder = new ImageView(this);
FrameLayout ladderFL = new FrameLayout(this);
View dummyView = new View(this);
ladder.setImageResource(R.drawable.ladder1);
LL.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0);
ladderFLParams.weight = 5f;
LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(0,0);
dummyParams.weight = 1f;
FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
ladder.setLayoutParams(ladderParams);
ladderFL.setLayoutParams(ladderFLParams);
dummyView.setLayoutParams(dummyParams);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);
ladderFL.addView(ladder);
LL.addView(ladderFL);
LL.addView(dummyView);
((RelativeLayout) findViewById(R.id.screenRL)).addView(LL);
解决方案
LinearLayout LL = new LinearLayout(this);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);
ImageView ladder = new ImageView(this);
ladder.setImageResource(R.drawable.ic_launcher);
FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
ladder.setLayoutParams(ladderParams);
FrameLayout ladderFL = new FrameLayout(this);
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0);
ladderFLParams.weight = 5f;
ladderFL.setLayoutParams(ladderFLParams);
ladderFL.setBackgroundColor(Color.GREEN);
View dummyView = new View(this);
LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
dummyParams.weight = 1f;
dummyView.setLayoutParams(dummyParams);
dummyView.setBackgroundColor(Color.RED);
ladderFL.addView(ladder);
LL.addView(ladderFL);
LL.addView(dummyView);
RelativeLayout rl=((RelativeLayout) findViewById(R.id.screenRL));
rl.addView(LL);
I have just arranged your code for better understanding, also gave it a background color to get the clear picture as I dont know what you want, you can go through it. I Hope it is helpful. You should provide your working xml so we know exactly what you want.
这篇关于添加的LinearLayout编程方式在Android不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文