以编程方式设置图像名称和位置

时间:2012-02-28 14:20:05

标签: android

在我硬编码图像视图和名称的位置之前,但现在我从解析器中检索它

我有位置的ArrayList,即我从xml中检索的X,y和Image名称。现在我想将它分配给我的main3.xml布局,该布局具有相对布局,并且没有像我没有指定位置和名称的子视图。

我是否需要夸大布局或任何其他想法?或者我应该以编程方式制作10个图像视图

这是我的main3.xml布局,这是硬编码的,现在我想通过代码给那些topmargin_left和top ..

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/Level1Layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="objectClick" 
            android:background="@drawable/hh_gmw_03">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="593dp"
        android:layout_marginLeft="998dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image1"
        android:src="@drawable/hht3l01"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="39dp"
        android:layout_marginLeft="20dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image1"
        android:src="@drawable/hht3l02" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="153dp"
        android:layout_marginLeft="497dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image3"
        android:src="@drawable/hht3l03" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="524dp"
        android:layout_marginLeft="998dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image4"
        android:src="@drawable/hht3l04" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="183dp"
        android:layout_marginLeft="715dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image5"
        android:src="@drawable/hht3l05" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="413dp"
        android:layout_marginLeft="557dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image6"
        android:src="@drawable/hht3l06" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="53dp"
        android:layout_marginLeft="532dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image7"
        android:src="@drawable/hht3l07" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您仍然可以继续使用main3.xml布局并以编程方式设置边距:

  1. 使用Resources.getIdentifier(..)方法查找每个ImageView的标识符,其中“name”参数将在某个循环中生成为"imageView" + i
  2. 使用ViewGroup.MarginLayoutParams.setMargins(..)
  3. 为每个ImageView设置布局参数
  4. 在ImageViews的父级上调用View.invalidate(),在您的情况下,使用Level1Layout id RelativeLayout