0

我试图制作一个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);
4

1 回答 1

0

我认为您可以使用AspectRatioLayout

<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>
于 2015-08-01T14:07:36.647 回答