以编程方式将图像添加到RelativeLayout

5
我希望通过代码将多个相对布局添加到线性布局中,每个相对布局包括:一个在左侧的ImageView,一个正好居中在其右侧的TextView和另一张在右侧的Image。我必须使用从数据库读取的数据添加它们。它必须使用RelativeLayout,因为我想在图像上使用一个监听器,在RL上使用不同的监听器。
每个RelativeLayout看起来像我在XML中有的东西。
<RelativeLayout
    android:id="@+id/relativecanal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:src="@drawable/antena3" />

       <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerVertical="true"
           android:layout_marginLeft="39dp"
           android:layout_toRightOf="@+id/imageView1"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge" />

       <ImageView
           android:id="@+id/imageView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"
           android:src="@drawable/star" />

    </RelativeLayout>

我正在使用以下代码:但是在添加图片的addView()时,我遇到了NullPointerException错误,我尝试只添加textView并且它可以工作。

    try {
     File sdcard = Environment.getExternalStorageDirectory();
     File file = new File(sdcard,archivoCanales);
  LinearLayout layout = (LinearLayout) findViewById(R.id.channelslistlayout);
     BufferedReader br = new BufferedReader(new FileReader(file));
     String line;



     while ((line = br.readLine()) != null) {



         RelativeLayout channel=new RelativeLayout(this);
         channel.setBackgroundColor(2);

         TextView channelName=new TextView(this);
         channelName.setText(new String(line));

         ImageView image=(ImageView)findViewById(R.drawable.animalplanet);


      LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT,
              LinearLayout.LayoutParams.WRAP_CONTENT
      );    

    RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            firstImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


         RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                secImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);


         channel.addView(image,firstImageParams);
         channel.addView(channelName,secImageParams);

         layout.addView(channel,llp);
}

 }
 catch (IOException e) {
    e.printStackTrace();
 }
2个回答

6

您正在使用 (ImageView)findViewById(R.drawable.animalplanet),但据我所知,这不是编程中正确的方式。如果您想要填充一个 ImageView,应该像这样做:

RelativeLayout.LayoutParams imParams = 
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
imSex.setId(2);
addView(imSex,imParams);    

然而,如果你只想展示图片而不需要从数据库中加载,并且不可点击,我建议你使用ViewBinder


1
image.setImageResource(R.drawable.animalplanet); 这是正确的方式。谢谢! - danywarner
这里是关于编程的内容。addView()方法是什么?我遇到了一个错误:“在AddPhotoActivity类型中未定义addView(ImageView, LinearLayout.LayoutParams)方法”。 - rams
@venkataramana 如果你是在Activity中执行此操作,那么它会是这样的:relativeLayout.addView(imSex,imParams)。 - Nayanesh Gupte
我从另一个问题得到了这个答案,但你的答案仍然很棒,+1。 - Hamza

4

你没有创建一个新的图像,所以会出现空指针异常。你应该要么创建一个新的图像,要么布局填充一个新的图像。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接