gpt4 book ai didi

Android - XML 布局与编程

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:44 24 4
gpt4 key购买 nike

我正在尝试将相对布局与扩展 View 的自定义类和几个按钮一起使用。这就是我最终希望它看起来像的样子:

http://imgur.com/B5MtdJ7

(请原谅我发的是链接而不是图片,显然我还不够酷)

目前这是用 dp 中的高度硬编码的(见下面的 XML),这有两个问题:

  • 它只在我的 Nexus 7 屏幕上看起来可以接受,在其他设备上不行
  • 两个自定义 View 的 onDraw 方法仍然提供高度和宽度与设备分辨率完全匹配的 Canvas

如果我尝试将 layout_height 设置为 wrap_content,每个自定义 View 都会尝试占据整个屏幕,这似乎与要点 2 一致,但显然不是我想要的。

我想要实现的是具有两个自定义 View 的相对布局,它看起来与图片中显示的完全相同,但会根据其所在屏幕的尺寸自行缩放,并且自定义 View Canvas 实际上知道有多大它所在的屏幕。我该怎么做?

编辑:这是“vs programmatic”的原因是我认为覆盖测量不会是一个糟糕的喊叫,但我不知道这将如何与 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>

最佳答案

使用 LinearLayout 和权重这会相当容易。我在下面给出了一个例子,但我现在无法测试它。不过,它应该提供一些方向。

<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" >

<com.sportsim.virtualcricket.view.SideProfileView
android:id="@+id/side_profile_view"
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="0dp"
android:layout_weight="0.25" />

<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>
</LinearLayout>

关于Android - XML 布局与编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com