通过Android中的JAVA向布局添加组件

时间:2014-02-25 19:02:46

标签: android relativelayout

实际上,主要布局是相对布局,实习生在其中包含相对布局。主要目标是将图像添加到内部相对布局中。

我试过这个但没有成功。这是我的代码:

import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Point;

public class MainActivity extends Activity {

    Display display;
    Point point;
    int width;
    int height;
    Animation up, down, right, left;
    RelativeLayout rl, rl2;


    Integer[] pics = { R.drawable.img1, R.drawable.img2,R.drawable.img3, R.drawable.img4,
            R.drawable.img5, R.drawable.img6,R.drawable.img7, R.drawable.img8,
            R.drawable.img9, R.drawable.img10,R.drawable.img11, R.drawable.img12,
            R.drawable.img13, R.drawable.img14,R.drawable.img15};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Init();

    }

    private void Init() {
        // TODO Auto-generated method stub


        rl = (RelativeLayout) findViewById(R.id.Rlayout);
        rl.setGravity(Gravity.CENTER);
        int x = 0;
        int y = 0;

        for(int i = 1 ; i<16 ; i++ ){
            ImageView img = new ImageView(this);
            img.setImageResource(pics[i-1]);
            int tempx = img.getWidth();
            int tempy = img.getHeight();
            rl.addView(img, x, y);
            x += tempx;
            if(i%4 == 0){
                x = 0;
                y += tempy;
            }

        }
        rl.bringToFront();

        }
}

正在使用的布局的XML代码是:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/R1Layout">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:id="@+id/Rlayout" >
    </RelativeLayout>

</RelativeLayout>

我得到的输出是一个空白屏幕。

帮助我解决如何在内部相对布局中显示图像的方法

提前致谢,

2 个答案:

答案 0 :(得分:1)

要将图像插入内部RelativeLayout,您需要使用ImageView对象。您可以使用RelativeLayout可用的addView()方法(通过从ViewGroup类继承)来实现。您可以在活动中按照以下方式执行此操作(您必须决定将其放置在何处):

RelativeLayout rl = (RelativeLayout) findViewById(R.id.Rlayout); // find inner layout
ImageView iview = new Image(this);
// Set image view parameters - refer to Android documentation for this
rl.addView(iview); // and so on. 
// You can add as many items as you want to any layout you want in this manner

答案 1 :(得分:0)

我认为你是误解,x和y不是坐标,分别是宽度和高度

rl.addView(img, x, y);

并且您无法获得图片视图的大小:

int tempx = img.getWidth();
int tempy = img.getHeight(); 

您必须使用容器来设置图像视图的大小和坐标:

 RelativeLayout.LayoutParams params;
                params = new RelativeLayout.LayoutParams(100, 100);             
                params.leftMargin = x;
                params.topMargin = y;
                rl.addView(img,params);

这是一个如何做的例子:

private void Init() {
    // TODO Auto-generated method stub

    int x = 0;
    int y = 0;
    rl = (RelativeLayout) findViewById(R.id.Rlayout);
    rl.setGravity(Gravity.CENTER);
      for(int i = 0 ; i<14 ; i++ ){
            ImageView img = new ImageView(this);
            img.setImageResource(pics[i]);
                //int tempx = img.getWidth();
            //int tempy = img.getHeight();           
            int tempx = 100;                
            int tempy = 100;
            //rl.addView(img, x, y);

            x += tempx;
            if(i%4 == 0){
                x = 0;
                y += tempy;
            }

            RelativeLayout.LayoutParams params;
            params = new RelativeLayout.LayoutParams(100, 100);             
            params.leftMargin = x;
            params.topMargin = y;
            rl.addView(img,params);



        }                       
    rl.bringToFront();

}
和Rajesh一样,我也建议使用Gridview代替RelativeLayout

见这个例子:

GridView and excess space padding