2015-04-02 127 views
0

我有一個ImageViewTextViewRelativeLayout嵌套在ScrollView內。我無法將ImageView的頂部與TextView的頂部對齊。它從文本視圖頂部邊緣向下移動約150dp。無法將一個ImageView的頂部對齊到TextVIew的頂部

這裏是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/transitionForm" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/backgroundcolor" 
android:orientation="horizontal" > 

<ScrollView 
    android:id="@+id/svTutorial" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:splitMotionEvents="true" 
    android:layout_above="@+id/ImageBottom" 
    android:layout_below="@+id/ImageTop" 
    android:scrollbars="vertical" 
    > 

<RelativeLayout 
    android:id="@+id/layoutTransitionPicWidgets" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="horizontal"> 


    <TextView 
     android:id="@+id/lblQuestionText" 
     android:layout_width="250dp" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="30px" 
     android:layout_marginLeft="50px" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="25px" 
     android:layout_alignParentLeft="true" 
     android:layout_weight="1" 
     android:text="get text from db." 
     android:textColor="#000000" 
     android:textSize="20sp" /> 

    <ImageView 
     android:id="@+id/imgPic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/slidetest" 

     /> 

</RelativeLayout> 
</ScrollView> 
</RelativeLayout> 

我想以編程方式更改基於其尺寸圖像的寬度,所以我編程所有參數的設置。

下面是代碼:

imgPic = (ImageView)findViewById(R.id.imgPic); 

    Display display = getWindowManager().getDefaultDisplay(); 
    DisplayMetrics outMetrics = new DisplayMetrics(); 
    display.getMetrics(outMetrics); 

    final float density = getResources().getDisplayMetrics().density; 
    final float dpHeight = outMetrics.heightPixels/density; 
    final float dpWidth = outMetrics.widthPixels/density; 

    ViewTreeObserver vto = imgPic.getViewTreeObserver(); 
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 
      imgPic.getViewTreeObserver().removeOnPreDrawListener(this); 


      final int finalHeight = imgPic.getMeasuredHeight(); 
      final int finalWidth = imgPic.getMeasuredWidth(); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      params.width = Math.round(.4f * dpWidth * density); 
      params.leftMargin = 50; 
      params.topMargin = 0; 
      params.rightMargin = 25; 
      //params.gravity= Gravity.TOP; 
      params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
      params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
      params.addRule(RelativeLayout.RIGHT_OF, R.id.lblQuestionText); 

      imgPic.setLayoutParams(params); 

      return true; 
     } 
    }); 

我試着用的LinearLayout這樣做的,得到相同的結果。我錯過了什麼?

+0

如果使用滾動視圖,然後更好的一套高度WRAP_CONTENT代替fill_parent – Apurva 2015-04-03 07:25:12

回答

0

您可以添加到您的ImageView

android:layout_alignTop="@+id/lblQuestionText" 

,但我認爲還有更重要的是怎麼回事。您沒有在ImageView上指定任何對齊屬性,因此它可能無法佈局您認爲將要佈局的位置。

好像也許你想要的ImageView要到TextView的權利,在這種情況下,添加該到ImageView應該有所幫助:

android:layout_toRightOf="@+id/lblQuestionText"