2013-03-21 62 views
5

我想使用扩展视图的自定义类的相对布局,以及一些按钮。这就是我最终希望它看起来像:Android - XML布局与编程

http://imgur.com/B5MtdJ7

(原谅我张贴的链接,而不是图像,显然我没有足够的冷静还)

目前,这是用硬编码在DP高度(见下文XML),还有两个问题:

  • 它只是看起来我的Nexus 7的屏幕上可以接受的,并没有其他设备
  • 两个自定义视图的onDraw满足HOD还提供了一个画布高度和宽度,该设备

的分辨率完全匹配。如果我尝试设置layout_height为wrap_content,每个自定义视图企图占据整个屏幕,这似乎与子弹2点一致,但显然不是我想要的。

我想要实现的是具有两个自定义视图的相对布局,它看起来完全如图所示,但会将其自身缩放到它所坐的屏幕的尺寸上,并且自定义视图画布实际上知道有多大它坐落在屏幕的一部分。我该怎么做呢?

编辑:这是“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> 

回答

1

这将是使用的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> 
+0

谢谢,当我回家时,我会给出一个去。这会保持按钮的状态(即每个占用屏幕宽度的一半,并且彼此相邻吗?) 不是说这是一件大事,因为这只是一个测试活动,但我想了解我可以从这个 – Imran 2013-03-21 13:17:10

+0

好吧,我会修改它,所以按钮是并排的。现在应该可以。再次,我没有测试过它,通常嵌套的权重对性能不利。虽然你可以用LinearLayout替换RelativeLayout来设置按钮的位置。 – 2013-03-21 13:18:11

+0

好啊,是的RelativeLayout的事情听起来不错 - 欢呼的人:D – Imran 2013-03-21 13:42:57