【问题标题】:Adding components to a layout via JAVA in android在android中通过JAVA向布局添加组件
【发布时间】:2014-03-28 05:42:39
【问题描述】:

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

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

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>

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

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

提前致谢,

【问题讨论】:

  • 使用网格视图代替RelativeLayout

标签: android android-relativelayout


【解决方案1】:

我想你误会了,x和y不是坐标,分别是宽度和高度

rl.addView(img, x, y);

并且您无法通过以下方式获得图像视图的大小:

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

您必须使用容器为您的 imageview 设置大小和坐标:

 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

【讨论】:

  • 谢谢。但我要使用相对布局的唯一原因是 api 版本。因为 api 级别 14 或更高版本支持网格布局。但我希望我的应用程序能够在 api 级别 11 及以上的手机中运行。你能详细说明如何使用 ImageView 吗?我正在使用上面的图像视图来实现。你能区分一下我上面的代码吗?
【解决方案2】:

要将图像插入内部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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2013-09-08
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多