【问题标题】:Android Impossibile fill Layout height with custom viewAndroid Impossibile 使用自定义视图填充布局高度
【发布时间】:2012-12-24 02:56:46
【问题描述】:

我创建了一个自定义视图,并将其放在 LinearLayout 上。

问题是我不能完全填满layout的高度,自定义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并使用两者“拉伸”视图

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

但是,如果我尝试获取尺寸高度,它会返回与之前相同的长度。

【问题讨论】:

  • 使用 android:layout_height="fill_parent" 尽管使用 android:layout_height="match_parent"

标签: android view


【解决方案1】:

问题出在视图的定义中,而不是在 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);
    }

而且,很自然,它将 View 的大小设置为一个正方形,其长度为 Minor 的长度。

这是正确的代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多