[英]ConstraintLayout: align top of textview with imageview
[英]Can't get the top of one ImageView to align to top of TextVIew
我在嵌套在ScrollView
內的RelativeLayout
中有一個ImageView
和TextView
。 我無法使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
上指定任何對齊方式屬性,因此它可能不會在您認為要布局的位置進行布局。
似乎您希望ImageView
在TextView
的右側,在這種情況下,將其添加到ImageView
應該會有所幫助:
android:layout_toRightOf="@+id/lblQuestionText"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.