Android:我正在尝试将新视图添加到RelativeLayout中

时间:2015-07-29 06:46:14

标签: android android-layout

我想以编程方式将新的ImageView或其他视图添加到一个realative布局中,我需要在向布局添加新视图后使用setcontentView(),当我调用setcontentView()时,我是否可以使用空的realtivelayout时间并以编程方式添加视图,在此先感谢,请给我完整的代码。

MainActivity.Java;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView im = (ImageView)findViewById(R.id.imageView1);
    im.setImageResource(R.drawable.a5);

    ImageView im2 = new ImageView(this);
    im2.setId(1);
    im2.setMinimumWidth(50);
    im2.setMaxWidth(50);
    im2.setMinimumHeight(50);
    im2.setMaxHeight(50);
    im2.setImageResource(R.drawable.a4);
    Toast.makeText(this, "Success1", Toast.LENGTH_LONG).show();

    RelativeLayout rt= (RelativeLayout)findViewById(R.layout.activity_main);
    rt.addView(im2);

    /*  RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.activity_main);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button b1 = new Button(this);
    b1.setText("Clcik me");
    b1.setLayoutParams(params);
    b1.setId(1);
    mainLayout.addView(b1); */

    /*ImageView displayImage =new  ImageView(this);
    displayImage.setImageResource(R.drawable.a1);

    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.activity_main);
    RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    displayImage.setLayoutParams(lparams);
    displayImage.setId(1);
    mainLayout.addView(displayImage);

    setContentView(mainLayout);*/

}

我已经尝试了评论中的内容但没有成功 我的布局:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
    />
</RelativeLayout> 

2 个答案:

答案 0 :(得分:1)

我认为你想要做的是在相对布局中膨胀动态创建的图像视图。贝娄是一个示例代码,我希望它有所帮助..

在activity_main中:

- (void)buttonClicked {
   [self setTexture:[SKTexture textureWithImageNamed:"buttonClicked.png"]];
}

在MainActivity中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    <!-- give this layout an id -->
    android:id="@+id/rt"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"

</RelativeLayout> 

答案 1 :(得分:0)

在setContentView();

中传递你的relativelayout