Android 以编程方式创建LinearLayout
示例
层次结构
- LinearLayout(horizontal) - ImageView - LinearLayout(vertical) - TextView - TextView
码
LinearLayout rootView = new LinearLayout(context); rootView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); rootView.setOrientation(LinearLayout.HORIZONTAL); //用于图像查看 ImageView imageView = new ImageView(context); //用于水平线性布局 LinearLayout linearLayout2 = new LinearLayout(context); linearLayout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); linearLayout2.setOrientation(LinearLayout.VERTICAL); TextView tv1 = new TextView(context); TextView tv2 = new TextView(context); //将2个textview添加到水平linearlayout linearLayout2.addView(tv1); linearLayout2.addView(tv2); //最后,将imageview和水平linearlayout添加到垂直linearlayout(rootView) rootView.addView(imageView); rootView.addView(linearLayout2);