Share
Embed
Transcript
59 slides
-
Activity application presentation layer single, focused things that user can
do rough synonym of “screen” -
src/**/MainActivity.java public class MainActivity extends Activity { ! @Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } -
supports consistent navigation and view switching (tabs or drop-down lists)
http://developer.android.com/guide/topics/ui/actionbar.html ActionBar dedicated space for app identity and user's location makes important actions accessible (such as Search) -
An action bar that includes the [1] app icon, [2]
two action items, and [3] action overflow. -
View is java class a visual application component on a
screen http://developer.android.com/reference/android/view/View.html onMeasure() onLayout() onDraw() with methods: -
Example @Override protected void onCreate(Bundle state) { super.onCreate(state); TextView view
= new TextView(this); view.setText("Hello World"); setContentView(view); } -
Context access to application-specific resources and classes for launching activities,
broadcasting and receiving intents, etc. access to up-calls for application-level operations -
Example @Override protected void onCreate(Bundle state) { super.onCreate(state); mapView =
new MapView(this); mapView.onCreate(state); setContentView(mapView); } -
LinearLayout LinearLayout layout = new LinearLayout(this); layout.addView(new Button(this)); layout.addView(new Button(this));
layout.addView(new Button(this)); Session 4 Session 4 Horizontal Vertical -
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> ! <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=“1"/> ! …2 more buttons.. ! </LinearLayout> res/layout/main.xml https://gist.github.com/LArchaon/4ea1eaa89c21ceccda10 -
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> ! < android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=“1"/> ! …2 more buttons.. ! </ res https://gist.github.com/LArchaon/4ea1eaa89c21ceccda10 android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_height="wrap_content" -
in Activity Layout resource from res/ @Override protected void onCreate(Bundle
state) { super.onCreate(state); setContentView(R.layout.main); } -
@Override protected void onCreate(Bundle state) { super.onCreate(state); View view =
LayoutInflater.from(this) .inflate(R.layout.main, null); setContentView(view); } same as -
LayoutInflater 1. fill (a balloon, tyre, or other expandable structure)
with air or gas so that it becomes distended. 2. increase (something) by a large or excessive amount. View view = LayoutInflater.from(this) .inflate(R.layout.activity_main, null); inflate! /ɪnˈfleɪt/! verb -
LayoutInflater 1. fill (a balloon, tyre, or other expandable structure)
with air or gas so that it becomes distended. 2. increase (something) by a large or excessive amount. View view = LayoutInflater.from(this) .inflate(R.layout.activity_main, null); inflate! /ɪnˈfleɪt/! verb Fill View from XML -
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> ! <Button android:id=“@+id/button” android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text=“1"/> ! …2 more buttons.. ! </LinearLayout> res/layout/main.xml -
@Override protected void onCreate(Bundle state) { super.onCreate(state); View view =
LayoutInflater.from(this) .inflate(R.layout.main, null); Button button = (Button) view.findViewById(R.id.button); button.setText("Button"); setContentView(view); } View.findViewById -
@Override protected void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.main); Button button
= (Button) findViewById(R.id.button); button.setText("Button"); } Activity.findViewById -
<LinearLayout ….. android:orientation="horizontal" android:weightSum="2"> ! <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1"/>
! <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"/> ! </LinearLayout> -
<LinearLayout ….. android:orientation="horizontal" android:weightSum="3"> ! <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="1"/>
! <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"/> ! </LinearLayout> -
<LinearLayout ….… android:weightSum="5"> ! <Button ….. android:layout_weight="1" android:text="1"/> <LinearLayout android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_weight=“3" android:orientation="vertical"> </LinearLayout> ! <Button … android:layout_weight="1" android:text="2"/> ! </LinearLayout> -
<LinearLayout …… android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="0.5" android:text="3"/> ….. Button with text 4 …… </LinearLayout> ! <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5"/> </LinearLayout> https://gist.github.com/LArchaon/b0ef20c1395c3b884b47 -
<RelativeLayout …. > <Button …. android:id="@+id/A" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button
…. android:id="@+id/F" android:layout_alignParentRight="true"/> <LinearLayout …. android:id="@+id/container" android:layout_toRightOf="@+id/A" android:layout_toLeftOf="@+id/F" android:weightSum="3"> <Button android:text="B" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> …. Buttons C and D ….. </LinearLayout> …. Button E ….. </RelativeLayout> https://gist.github.com/LArchaon/b6842ad6f4eadbfac012 -
Configuration qualifiers en, fr, en-rUS, fr-rFR, fr-rCA Language and region
mcc310, mcc310-mnc004 Mobile country code Screen size small, normal, large, xlarge Orientation land, port Screen density ldpi, mdpi, hdpi, nodpi -
Do not use hard-coded values in your application code (num,
strings) Use wrap_content, fill_parent, or the dp unit for layout dimensions Test Your Application on Multiple Screens ! -
View TextView ImageView Button ViewGroup LinearLayout RelativeLayout FrameLayout GridLayout AdapterView
Expandable ListView ListView GridView Spinner -
View TextView ImageView Button ViewGroup LinearLayout RelativeLayout FrameLayout GridLayout AdapterView
Expandable ListView ListView GridView Spinner Next Session