Android - XML ​​vs. software layout

I am trying to use a relative layout with a custom class extending the view and a few buttons. This is what I ultimately want it to look like this:

http://imgur.com/B5MtdJ7

(forgive me for posting the link, not for the image, apparently I'm not cool enough)

This is currently hardcoded with heights in dp (see XML below), and there are two problems with this:

  • It only looks on my Nexus 7 screen, and no other device
  • The onDraw custom views method still provides a canvas with height and width that exactly matches the device resolution

If I try to set layout_height to wrap_content, each user view will try to cover the entire screen, which seems to correspond to point 2 of the marker, but clearly not what I want.

What I want to achieve is a Relative Layout with two customizable views that look exactly as shown in the figure, but will scale to fit the size of the screen on which it sits. Custom canvas views really know how much of the screen it sits on. How to do it?

Edit: The reason this is “against the software” is because I believe that overriding in measure will not be a bad cry, but I don’t know how this will interact with XML. I would also prefer to define the layout in one place.

My XML is below:

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

+5
source
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>
+1

All Articles