1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 //Need the following import to get access to the app resources, since this 20 //class is in a sub-package. 21 import com.example.android.apis.R; 22 23 import android.app.Activity; 24 import android.os.Bundle; 25 import android.view.Menu; 26 import android.view.Gravity; 27 import android.view.MenuItem; 28 import android.widget.LinearLayout; 29 30 31 /** 32 * Demonstrates horizontal and vertical gravity 33 */ 34 public class LinearLayout8 extends Activity { 35 36 private LinearLayout mLinearLayout; 37 38 // Menu item Ids 39 public static final int VERTICAL_ID = Menu.FIRST; 40 public static final int HORIZONTAL_ID = Menu.FIRST + 1; 41 42 public static final int TOP_ID = Menu.FIRST + 2; 43 public static final int MIDDLE_ID = Menu.FIRST + 3; 44 public static final int BOTTOM_ID = Menu.FIRST + 4; 45 46 public static final int LEFT_ID = Menu.FIRST + 5; 47 public static final int CENTER_ID = Menu.FIRST + 6; 48 public static final int RIGHT_ID = Menu.FIRST + 7; 49 50 @Override onCreate(Bundle savedInstanceState)51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.linear_layout_8); 54 mLinearLayout = (LinearLayout)findViewById(R.id.layout); 55 } 56 57 @Override onCreateOptionsMenu(Menu menu)58 public boolean onCreateOptionsMenu(Menu menu) { 59 super.onCreateOptionsMenu(menu); 60 menu.add(0, VERTICAL_ID, 0, R.string.linear_layout_8_vertical); 61 menu.add(0, HORIZONTAL_ID, 0, R.string.linear_layout_8_horizontal); 62 menu.add(0, TOP_ID, 0, R.string.linear_layout_8_top); 63 menu.add(0, MIDDLE_ID, 0, R.string.linear_layout_8_middle); 64 menu.add(0, BOTTOM_ID, 0, R.string.linear_layout_8_bottom); 65 menu.add(0, LEFT_ID, 0, R.string.linear_layout_8_left); 66 menu.add(0, CENTER_ID, 0, R.string.linear_layout_8_center); 67 menu.add(0, RIGHT_ID, 0, R.string.linear_layout_8_right); 68 69 return true; 70 } 71 72 @Override onOptionsItemSelected(MenuItem item)73 public boolean onOptionsItemSelected(MenuItem item) { 74 switch (item.getItemId()) { 75 76 case VERTICAL_ID: 77 mLinearLayout.setOrientation(LinearLayout.VERTICAL); 78 return true; 79 case HORIZONTAL_ID: 80 mLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 81 return true; 82 83 case TOP_ID: 84 mLinearLayout.setVerticalGravity(Gravity.TOP); 85 return true; 86 case MIDDLE_ID: 87 mLinearLayout.setVerticalGravity(Gravity.CENTER_VERTICAL); 88 return true; 89 case BOTTOM_ID: 90 mLinearLayout.setVerticalGravity(Gravity.BOTTOM); 91 return true; 92 93 case LEFT_ID: 94 mLinearLayout.setHorizontalGravity(Gravity.LEFT); 95 return true; 96 case CENTER_ID: 97 mLinearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL); 98 return true; 99 case RIGHT_ID: 100 mLinearLayout.setHorizontalGravity(Gravity.RIGHT); 101 return true; 102 103 } 104 return super.onOptionsItemSelected(item); 105 } 106 } 107