2016-08-22 76 views
-1

中的设备线性布局我有4个图像在linearlayout内以编程方式创建,我想在第四个图像上设置textview如何设置??如果你有一块代码请分享跟我??如何设置4个图像为线性布局根据android

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300, 1.0f); 

public void path(ArrayList<Integer> integerImg) { 
    // int width=mContext.getWgetWindowManager().getDefaultDispaly.getWidth(); 
    ImageView images; 
    for (int i = 0; i < integerImg.size(); i++) { 
     if (i < 4) { 
      images = new ImageView(mContext); 
      params.setMargins(10, 0, 0, 0); 
      images.setLayoutParams(params); 
      images.setScaleType(ImageView.ScaleType.FIT_XY); 
      images.setBackgroundResource(R.drawable.salon_profile_back); 
      images.setImageResource(integerImg.get(i)); 
      linearImgGrid.addView(images); 
     } 
+0

能否请您与我们分享您的代码,并告诉我们,不能正常工作的一部分。 – peshkira

+0

看到上面的代码,我想在第4张图片上显示textview? – Shreyas

回答

0

试试这个

TextView tv= new TextView(this); 
      tv.setText("hallo hallo"); 
      tv.setId(5); 
      tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
    //add the 4th linear layout 
      ((LinearLayout) linearLayout).addView(tv); 
+0

我不明白这一点??你可以请编辑我的代码与我的代码,并发送给我。 – Shreyas

-1

可以在LinearLayout使用下面的代码以供参考

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="96dp" 
      android:layout_marginTop="70dp" 
      android:weightSum="3" 

      android:orientation="horizontal" > 


      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:layout_weight="1" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:orientation="vertical" > 


      <ImageButton 
       android:id="@+id/imageButton1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/user" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Master" /> 

</LinearLayout> 
      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:layout_weight="1" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:orientation="vertical" > 


      <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/report1" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Report" /> 

</LinearLayout> 

      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:layout_marginRight="@dimen/feed_item_margin" 
      android:weightSum="2" 
      android:layout_weight="1" 
      android:orientation="vertical" > 
      <ImageButton 
       android:id="@+id/imageButton3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/expense1" /> 
      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Expenses" /> 

</LinearLayout> 

    </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="96dp" 
      android:layout_marginTop="70dp" 
      android:weightSum="3" 
      android:orientation="horizontal" > 


      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:layout_weight="1" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:orientation="vertical" > 


      <ImageButton 
       android:id="@+id/imageButton4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/collection" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Collection" /> 

</LinearLayout> 
      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:layout_weight="1" 
      android:orientation="vertical" > 


      <ImageButton 
       android:id="@+id/imageButton5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/monthly" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Report" /> 

</LinearLayout> 

      <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:layout_weight="1" 
      android:layout_marginLeft="@dimen/feed_item_margin" 
      android:layout_marginRight="@dimen/feed_item_margin" 
      android:background="@drawable/bg_parent_rounded_corner" 
      android:orientation="vertical" > 


      <ImageButton 
       android:id="@+id/imageButton6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="center" 
       android:background="@null" 
       android:src="@drawable/extra" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:text="Expenses" /> 

</LinearLayout> 

    </LinearLayout> 
    </LinearLayout> 
+0

请发布问题的相关位,解释您在代码块外发布的内容。 –

+0

感谢您的代码Omkar帮助我... – Shreyas

0

而不是添加ImageView,你可以创建一个RelativeLayout把一个ImageView内和TextView并添加RelativeLayoutLinearLayout,因为这样:

RelativeLayout.LayoutParams layoutParamsImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
      RelativeLayout.LayoutParams.MATCH_PARENT); 
    RelativeLayout.LayoutParams layoutParamsText = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    layoutParamsText.addRule(RelativeLayout.CENTER_IN_PARENT); 


    for (int i = 0; i < integerImg.size(); i++) { 
     if (i < 4) { 

      RelativeLayout relativeLayout = new RelativeLayout(mContext); 
      relativeLayout.setLayoutParams(params); 




      ImageView images = new ImageView(mContext); 
      params.setMargins(10, 0, 0, 0); 
      images.setLayoutParams(layoutParamsImage); 
      images.setScaleType(ImageView.ScaleType.FIT_XY); 
      images.setBackgroundResource(R.drawable.salon_profile_back); 
      images.setImageResource(integerImg.get(i)); 
      relativeLayout.addView(images); 

      TextView textView = new TextView(mContext); 
      textView.setLayoutParams(layoutParamsImage); 
      textView.setText("My Text"); 
      relativeLayout.addView(textView); 

      linearImgGrid.addView(relativeLayout); 
     } 
    } 
+0

@Shreyas它有用吗? – LaurentY