2013-10-28 57 views
0

我試圖通過編程添加一些控件並設置動態添加的控件的位置。在Android上動態添加視圖和定位

我的佈局文件:

<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"/> 

,我使用添加和位置控制的代碼是:

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); 

    } 

控件添加到用戶界面,但他們忽略了左,頂級屬性。如果有人能幫我弄清楚問題出在哪裏,我將不勝感激。

回答

0

p2.topMargin在視圖本身內部設置邊距,並且不會影響父視圖中的視圖位置RelativeLayout

如果您想將ImageView放置在RelativeLayout中,您可以使用例如。 p2.addRule(RelativeLayout.LEFT_OF, iView1.getId())

相關問題