我正在尝试使用继承自View的自定义类和一些按钮来使用相对布局。这是我最终想要的效果:
抱歉我发了一个链接而不是图片,显然我还不够酷
目前这个高度是以dp为单位硬编码的(见下面的XML),这里有两个问题:
- 只有在我的Nexus 7屏幕上看起来可以接受,在其他设备上不行
- 两个自定义视图的onDraw方法仍然提供了一个具有与设备分辨率完全匹配的高度和宽度的画布
如果我尝试将layout_height设置为wrap_content,则每个自定义视图都会尝试占用整个屏幕,这似乎符合第2点,但显然不是我想要的。
我想要实现的是一个相对布局,其中包含两个自定义视图,看起来与图片完全相同,但会根据其所在屏幕的尺寸进行缩放,并且自定义视图的画布实际上知道它所占屏幕的大小。我该如何做到这一点?
编辑:之所以这是“vs程序化”,是因为我认为重写on measure可能不是一个坏主意,但我不知道它会如何与XML交互。我宁愿在一个地方定义布局。
我的XML如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TrajectoryView" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
<com.sportsim.virtualcricket.view.SideProfileView
android:id="@+id/side_profile_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.sportsim.virtualcricket.view.SideProfileView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
<com.sportsim.virtualcricket.view.BirdsEyeView
android:id="@+id/birds_eye_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Button" />
</LinearLayout>