我试图制作一个RelativeLayout
纵横比为 2:1 的图像。ImageView
布局正在正确调整大小,而且RelativeLayout
看起来也不错。但TextView
不可见,这是因为属性 android:layout_centerInParent="true"
使用任何对齐、居中等属性的父级导致该视图不可见。如何解决?
我的相对布局:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(widthMeasureSpec/2, MeasureSpec.EXACTLY));
}
<com.stikyapp.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@color/green"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="textView1"/>
</com.stikyapp.MyRelativeLayout>
如果我使用的是正方形的纵横比 1:1,那么它都适用于这段代码
super.onMeasure(widthMeasureSpec, widthMeasureSpec);