无法将一个ImageView的顶部对齐到TextVIew的顶部

时间:2015-04-02 22:41:48

标签: android android-layout

ImageView内的TextViewRelativeLayout嵌套在ScrollView内。我无法让ImageView的顶部与TextView的顶部对齐。它从文本视图顶部边缘向下徘徊约150dp。

这是我的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执行此操作并获得相同的结果。我错过了什么?

1 个答案:

答案 0 :(得分:0)

您可以将其添加到ImageView

android:layout_alignTop="@+id/lblQuestionText"

但我认为还有更多的事情要发生。您没有在ImageView上指定任何对齐属性,因此可能无法布置您认为要进行布局的位置。

似乎您希望ImageView位于TextView的右侧,在这种情况下,将其添加到ImageView会有所帮助:

android:layout_toRightOf="@+id/lblQuestionText"