繁体   English   中英

无法获得一个ImageView的顶部以与TextVIew的顶部对齐

[英]Can't get the top of one ImageView to align to top of TextVIew

我在嵌套在ScrollView内的RelativeLayout中有一个ImageViewTextView 我无法使ImageView的顶部与TextView的顶部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这样做并获得相同的结果。 我想念什么?

您可以将其添加到您的ImageView

android:layout_alignTop="@+id/lblQuestionText"

但我认为还有更多的事情要做。 您无需在ImageView上指定任何对齐方式属性,因此它可能不会在您认为要布局的位置进行布局。

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

android:layout_toRightOf="@+id/lblQuestionText"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM