我使用的是具有背景图片的自定义视图,用于在其上绘制内容。
该视图忽略了android:layout_width =“ wrap_content和android:layout_height =” wrap_content,它没有包装背景图片,而是将视图下的控件拉伸并推出了屏幕。
自定义视图位于主要的LinearLayout中,但是无论我放在哪里,它的行为都相同。
我尝试了setLayoutParams,但是在可见控件下物理尺寸仍然是相同的,并且尺寸非常重要,因为我保存了其他时间的坐标。它甚至出现在自定义标题下,并且坐标作为负数返回,这也是一个问题。
由于屏幕尺寸不同,我不喜欢静态设置尺寸的想法。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<org.danizmax.tradar.MapView
android:id="@+id/mapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- koordinate -->
<LinearLayout android:id="@+id/controls"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10sp"
android:paddingRight="10sp">
<TextView android:id="@+id/coordsText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="15sp"
android:gravity="center"
android:layout_weight="1">
</TextView>
</LinearLayout>
<!-- radij -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10sp"
android:paddingLeft="-5sp"
android:paddingRight="-5sp">
<Button android:text=""
android:id="@+id/ButtonInc"
android:background="@drawable/leftbuttonani"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<TextView android:id="@+id/radiusSize"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="16sp"
android:gravity="center"
android:layout_weight="1"
android:paddingLeft="0sp"
android:paddingRight="0sp">
</TextView>
<Button android:text=""
android:id="@+id/ButtonDec"
android:background="@drawable/rightbuttonani"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
</LinearLayout>
<!-- alert togle -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="-5sp"
android:paddingRight="-5sp">
<Button android:text=""
android:id="@+id/ButtonDec"
android:background="@drawable/buttonani"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<ToggleButton
android:id="@+id/toggleAlert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#D8D8D8"
android:background="@drawable/buttonani"
android:textOn="@string/alertOn"
android:textOff="@string/alertOff"
android:textStyle="bold"
android:layout_weight="0.5"
android:paddingTop="1sp">
</ToggleButton>
<Button android:text=""
android:id="@+id/ButtonDec"
android:background="@drawable/buttonani"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
</LinearLayout>
<!-- izpis zadnjega dogodka -->
<TextView android:id="@+id/timeStatText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="10sp"
android:gravity="center">
</TextView>
这就是它的外观,蓝色部分是MapView:
任何想法?
最佳答案
通过覆盖onMeasure()解决了该问题:
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
if(parentWidth<parentHeight){
this.setMeasuredDimension(parentWidth, (int)(parentWidth*(float)factor));
}else{
this.setMeasuredDimension((int)(parentHeight/(float)factor), parentHeight);
}
}
在HERE中找到了更详细的答案。