[英]Align ImageView and TextView in a Linear Layout
我尝试将ImageView和TextView对齐,但是imageView高于textview。
Xml:
<RelativeLayout
android:layout_marginTop="8dp"
android:id="@+id/secondRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/firstRelative"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageNote"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/rank"
android:layout_marginEnd="2dp"
/>
<TextView
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Test"
android:layout_alignBaseline="@id/imageNote"
/>
</RelativeLayout>
我得到的预览:
而我在设备上得到的:
我想对齐ImageView和TextView,但不知道该怎么做。
编辑:
感谢@Bob Malooga和@Rethinavel Pillai。
我使用了TextView的setCompoundDrawables,效果很好! (而且对于perf更好)。
这是我的新代码:
<RelativeLayout
android:id="@+id/relativeDetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
/>
<TextView
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="@+id/time"
/>
<TextView
android:id="@+id/internetNeeded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="@+id/note"
/>
</RelativeLayout>
要在TextView中设置drawable:
TextView duree = (TextView) convertView.findViewById(R.id.time);
int colorG = Color.parseColor("#FFA500");
Float taille = Utils.convertDpInPx(16, activity);
/*Convert DP in Pixel
public static float convertDpInPx(int dp, Activity activity) {
Resources r = activity.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return px;
}*/
Drawable imgChrono = getContext().getResources().getDrawable( R.drawable.chrono );
imgChrono.setBounds( 0, 0, Math.round(taille), Math.round(taille)); //To change width and height of drawable
imgChrono.setColorFilter(colorG, PorterDuff.Mode.SRC_ATOP); //To change color of element
duree.setCompoundDrawables(imgChrono, null, null, null );
duree.setText(duration);
尝试这种方式:
<LinearLayout
android:layout_marginTop="8dp"
android:id="@+id/secondRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageNote"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/rank"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"/>
<TextView
android:id="@+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:layout_alignBaseline="@id/imageNote"
/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.