Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有纵横比的android布局无法显示指向父级的子级_Android_Android Layout_Onmeasure - Fatal编程技术网

具有纵横比的android布局无法显示指向父级的子级

具有纵横比的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

我正在尝试制作一个纵横比为2:1的
相对图。布局正在正确调整大小,并且此
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>