【问题标题】:Adding image programmatically to RelativeLayout以编程方式将图像添加到 RelativeLayout
【发布时间】:2012-05-03 16:39:06
【问题描述】:

我想通过代码添加到线性布局,各种相关布局。每个相对布局包括:左侧的图像视图,右侧的文本视图(正好在中间)和右侧的另一个图像。我必须使用从数据库读取的数据添加它们。它必须与 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();
 }

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    您正在使用(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

    【讨论】:

    • image.setImageResource(R.drawable.animalplanet);是正确的方法。谢谢!
    • 这里什么是 addView() 方法。我收到错误:“方法 addView(ImageView, LinearLayout.LayoutParams) 未定义类型 AddPhotoActivity”。
    • @venkataramana 它是 relativeLayout.addView(imSex,imParams);如果您在活动中执行此操作
    • 我从另一个问题中得到了这个答案,但仍然为您的精彩回答 +1
    【解决方案2】:

    您没有制作新图像,这就是您得到空指针异常的原因。您应该创建一个新的或布局膨胀一个新的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      相关资源
      最近更新 更多