2012-12-23 76 views
0

我创建了一个自定义视图,我把它放在一个LinearLayout上。Android Impossibile填充布局高度与自定义视图

问题是我无法完全填充布局的高度,自定义View的大小总是平方,width = height。

这里是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:layout_weight="0"> 

    <it.inav.graphics.MapView 
     android:id="@+id/map" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"/> 

    </LinearLayout> 

似乎是使用的RelativeLayout和“strecth”同时使用

 android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 

,但仍然认为上班,如果我尝试获得的唯一途径尺寸高度,它返回之前的相同长度。

+0

使用 机器人:layout_height = “FILL_PARENT” 尽管使用 机器人:layout_height = “match_parent” –

回答

0

问题出在视图的定义上,而不是在XML中。

我已经这样做了一段代码复制粘贴:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int measuredWidth = measure(widthMeasureSpec); 
     int measuredHeight = measure(heightMeasureSpec); 
     int d = Math.min(measuredWidth, measuredHeight); 
     setMeasuredDimension(d, d); 
    } 

,当然还有它设置查看与未成年人的长的正方形的大小。

这是正确的代码:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int measuredWidth = measure(widthMeasureSpec); 
     int measuredHeight = measure(heightMeasureSpec); 
     setMeasuredDimension(measuredWidth, measuredHeight); 
    }