在android中用java代码创建Android布局

时间:2015-11-07 10:20:05

标签: java android android-layout

如何在此处添加一个带有图像的TextView。我想在imageView下面添加一个TextView。

[ ][x][x][x][H][T]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]

2 个答案:

答案 0 :(得分:1)

     LinearLayout LL = new LinearLayout(this);
     LL.setOrientation(LinearLayout.VERTICAL);

     LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

     LL.setLayoutParams(LLParams);

           ImageView imageView = new ImageView(mContext);

           //....your imageview code.......


           TextView textView = new TextView(mContext);

           //....your textview code.......


     LL.addView(imageview);
     LL.addView(textview);

答案 1 :(得分:0)

您应该使用xml布局编辑器来创建Grid的行,并使用inflater来检索View Java的对象。以编程方式,类似

LinearLayout parent = new LinearLayout(context);

parent.setLayoutParams(new GridView.LayoutParams(400, 400));
parent.setOrientation(LinearLayout.VERTICAL);

TextView textView = new TextView(context);
textView.setId(1)
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setId(0);

parent.addView(imageView);
parent.addView(textView);

应该这样做。

parentyour getView将返回的内容。当然,您必须将演员从ImageView更改为LinearLayout。您可以将findViewById与0和1一起使用,分别检索ImageViewTextView,或getChildAt(0)getChildAt(1)来执行相同的操作