在Android上动态添加视图和定位

在Android上动态添加视图和定位,android,dynamic,Android,Dynamic,我试图添加一些控件,并以编程方式设置动态添加的控件的位置 我的布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/loginView" android:layout_width="match_parent" android:layout_height="m

我试图添加一些控件,并以编程方式设置动态添加的控件的位置

我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/myappappbackground1920"
tools:context=".MyAppLoginActivity" >

<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_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:contentDescription="@string/app_name"
    android:src="@drawable/loginheader1920" />

<EditText
    android:id="@+id/txtEmailLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="39dp"
    android:background="@android:color/white"
    android:ems="14"
    android:hint="@string/hint_email_or_username"
    android:inputType="textEmailAddress"
    android:minHeight="35dp"
    android:padding="5dp"
    android:textColor="@color/textbox_color"
    android:textColorHint="@color/textbox_color"
    android:textSize="@dimen/textbox_font_size" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtPasswordLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtEmailLogin"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="39dp"
    android:layout_marginTop="45dp"
    android:background="@android:color/white"
    android:ems="14"
    android:hint="@string/hint_password"
    android:inputType="textPassword"
    android:minHeight="35dp"
    android:padding="5dp"
    android:textColor="@color/textbox_color"
    android:textColorHint="@color/textbox_color"
    android:textSize="@dimen/textbox_font_size" >
</EditText>

<ImageButton
    android:id="@+id/btnWatchTutorialLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="0dp"
    android:background="#00000000"
    android:contentDescription="@string/app_name"
    android:scaleType="center"
    android:src="@drawable/watchtutoriallogin1920" />

<ImageButton
    android:id="@+id/btnExecuteLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/txtPasswordLogin"
    android:layout_below="@+id/txtPasswordLogin"
    android:layout_marginTop="7dp"
    android:background="#00000000"
    android:contentDescription="@string/btn_login"
    android:src="@drawable/btnloginloginview1920" />

<TextView
    android:id="@+id/lnkForgotPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtPasswordLogin"
    android:layout_below="@+id/btnExecuteLogin"
    android:layout_marginTop="28dp"
    android:text="@string/lnk_forgot_password"
    android:textColor="@color/white"
    android:textSize="@dimen/textbox_font_size" />

<ImageButton
    android:id="@+id/btnBackLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnExecuteLogin"
    android:layout_alignLeft="@+id/txtPasswordLogin"
    android:background="#00000000"
    android:src="@drawable/btnback_login_1920" android:contentDescription="@string/lnk_back"/>

控件被添加到ui中,但它们忽略左侧和顶部属性。如果有人能帮我找出问题所在,我将不胜感激。

p2.topMargin
设置视图本身内部的边距,并且对父视图内部视图的位置没有影响
RelativeLayout

如果您想将ImageView放置在RelativeLayout内,则必须使用例如。
p2.addRule(RelativeLayout.LEFT_OF,iView1.getId())

private void AddItems() {
    final Context context = getApplicationContext();
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.loginView);
    RelativeLayout.LayoutParams p0 = new RelativeLayout.LayoutParams(layout.getLayoutParams());
    RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(layout.getLayoutParams());
    RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(layout.getLayoutParams());

    p0.topMargin = 20;
    p0.leftMargin = 0;
    p0.width = 1280;
    p0.height = 135;

    p1.width = 62;
    p1.height = 64;
    p1.topMargin = 10;
    p1.leftMargin =10;

    p2.topMargin = 10;
    p2.leftMargin = 30;

    ImageView iView0 = new ImageView(context);
    iView0.setId(1);
    iView0.setImageResource(R.drawable.semi_transparent_bar);
    iView0.setLayoutParams(p0);

    ImageView iView1 = new ImageView(context);
    iView1.setId(2);
    iView1.setImageResource(R.drawable.icon_info);
    iView1.setLayoutParams(p1);

    TextView tView = new TextView(context);
    tView.setId(3);
    tView.setTextColor(getResources().getColor(R.color.white));
    tView.setText("This is a test message.");
    tView.setLayoutParams(p2);

    this.addContentView(iView0, p0);
    this.addContentView(iView1, p1);
    this.addContentView(tView, p2);

   }