2015-11-07 59 views
1

如何在此圖像中添加一個TextView和圖像。我想在imageView下添加一個TextView。在Android中使用java代碼創建Android佈局

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 

    if (convertView == null) { 
     imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(400, 400)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(8, 8, 8, 8); 
     } else { 
     imageView = (ImageView) convertView; 
    } 

    imageView.setImageResource(mThumbIds[position]); 

    return imageView; 
    } 
+1

你爲什麼不使用同一事物的XML編輯器,並使用吹氣以檢索了Java的對象? – Blackbelt

+0

只是試圖以其他方式去做。探索可能性。 :) – vikky

+0

如果你能幫到你。 – vikky

回答

0

您應該使用XML佈局編輯器創建網格的行和使用吹氣以檢索查看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); 

應該這樣做。

parent是什麼your getView將返回。當然,你必須將演員從ImageView更改爲LinearLayout。您可以使用findViewById用0和1,分別檢索ImageViewTextView,或getChildAt(0)getChildAt(1)

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