Advertisement
yo2man

Creating a Custom Layout Part 2

Jul 16th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. // Creating a Custom Layout Part 2
  2. // Now we need a textView to hold the temperature, but
  3. // we want it to be in the center of the first circle we added.
  4.  
  5.  
  6. // Step 1. Drag plain TextView over to the top left corner. Change id to: temperatureLabel, clear text and add placeholder text: "100".
  7.  
  8. Then add this inside the text view:
  9.  
  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:id="@+id/temperatureLabel"
  14.  
  15.     // to align the textView to the edge of the circleImageView
  16.         android:layout_alignTop="@+id/circleImageView"
  17.         android:layout_alignBottom="@+id/circleImageView"
  18.         android:layout_alignLeft="@+id/circleImageView"
  19.         android:layout_alignRight="@+id/circleImageView"
  20.  
  21.     // then center it to the center of the circleImageView
  22.     android:gravity="center"
  23.     // color the text
  24.     android:textColor="#f25019"
  25.         tools:text="100"/>
  26.  
  27. // Step 2. Now lets move the whole thing around and give it padding so it looks nice and centered.
  28. // Go to the top:
  29.  
  30. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  31.                 xmlns:tools="http://schemas.android.com/tools"
  32.                 android:layout_width="match_parent"
  33.                 android:layout_height="wrap_content"
  34.         tools:background="#ffaa00"
  35.         // add padding
  36.                 android:paddingLeft="64dp"
  37.                 android:paddingRight="32dp"
  38.                 android:paddingTop="4dp"
  39.                 android:paddingBottom="4dp"
  40.                 >
  41.  
  42.  
  43. //  Step 3. add android:paddingLeft="10dp" to dayNameLabel and iconImageView ]
  44. //  to spread the icon and texts out a bit to look nicer. [**]
  45.  
  46. // The Result:
  47.  
  48. <?xml version="1.0" encoding="utf-8"?>
  49. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  50.                 xmlns:tools="http://schemas.android.com/tools"
  51.                 android:layout_width="match_parent"
  52.                 android:layout_height="wrap_content"
  53.                 tools:background="#ffaa00"
  54.                 android:paddingLeft="64dp"
  55.                 android:paddingRight="32dp"
  56.                 android:paddingTop="4dp"
  57.                 android:paddingBottom="4dp"
  58.                 >
  59.  
  60.     <ImageView
  61.         android:layout_width="wrap_content"
  62.         android:layout_height="wrap_content"
  63.         android:id="@+id/circleImageView"
  64.         android:layout_alignParentTop="true"
  65.         android:layout_alignParentLeft="true"
  66.         android:layout_alignParentStart="true"
  67.         android:src="@drawable/bg_temperature"/>
  68.  
  69.     <ImageView
  70.         android:layout_width="wrap_content"
  71.         android:layout_height="wrap_content"
  72.         android:id="@+id/iconImageView"
  73.         android:src="@drawable/clear_day"
  74.         android:layout_centerVertical="true"
  75.         android:layout_toRightOf="@+id/circleImageView"
  76.         android:layout_toEndOf="@+id/circleImageView"
  77.         android:paddingLeft="10dp" // [**]
  78.         />
  79.  
  80.     <TextView
  81.         android:layout_width="wrap_content"
  82.         android:layout_height="wrap_content"
  83.         android:id="@+id/dayNameLabel"
  84.         android:layout_centerVertical="true"
  85.         android:layout_toRightOf="@+id/iconImageView"
  86.         android:layout_toEndOf="@+id/iconImageView"
  87.         android:textColor="#ffffffff"
  88.         android:textSize="20sp"
  89.         tools:text="Wednesday"
  90.         android:paddingLeft="10dp" // [**]
  91.         />
  92.  
  93.     <TextView
  94.         android:layout_width="wrap_content"
  95.         android:layout_height="wrap_content"
  96.         android:id="@+id/temperatureLabel"
  97.         android:layout_alignTop="@+id/circleImageView"
  98.         android:layout_alignBottom="@+id/circleImageView"
  99.         android:layout_alignLeft="@+id/circleImageView"
  100.         android:layout_alignRight="@+id/circleImageView"
  101.         android:gravity="center"
  102.         android:textColor="#f25019"
  103.         tools:text="100"/>
  104.  
  105. </RelativeLayout>
  106.  
  107. // this would look pretty good on most devices.
  108. // But we can't test it until we create a custom adapter that allows us to attach
  109. // our data to each item.
  110. // That's coming up next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement