具有纵横比的android布局无法显示指向父级的子级
我正在尝试制作一个纵横比为2:1的具有纵横比的android布局无法显示指向父级的子级,android,android-layout,onmeasure,Android,Android Layout,Onmeasure,我正在尝试制作一个纵横比为2:1的相对图。布局正在正确调整大小,并且此RelativeLayout中的ImageView看起来不错。但是TextView不可见,这是因为属性android:layout\u centerInParent=“true”使用父级的任何对齐、居中等属性导致该视图不可见。如何解决这个问题 MyRelativeLayout: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasure
相对图。布局正在正确调整大小,并且此RelativeLayout
中的ImageView
看起来不错。但是TextView
不可见,这是因为属性android:layout\u centerInParent=“true”
使用父级的任何对齐、居中等属性导致该视图不可见。如何解决这个问题
MyRelativeLayout:
@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>
我想你可以用
不确定是什么导致了您的问题,也许您应该挖掘相关的代码。作为一种解决方法,您可以尝试使用具有匹配父维度的FrameLayout包装TextView,并将TextView居中放置在该FrameLayout内
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
<com.sherlock.aspectratio.AspectRatioLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:widthHeightRatio="2" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your imageview -->
</RelativeLayout>
</com.sherlock.aspectratio.AspectRatioLayout>