2012/2013 учебный год
Упражнение 3
Счётчик нажатий клавиши. Пункты меню - изображения. Прозрачность всех изображений 30%. Выделенный пункт не прозрачен. Увеличенное изображение выбранного пункта в центре.
Внимание! Откройте файл
gen/org/ggpi/MyLayout02/R.java
и проверяйте после выполнения каждого из ниже приведённых соответствующих примеров, что в нём зарегистрированы объекты:
Самостоятельно определите: какие объекты здесь должны быть?
Указания к выполнению
Подготовьте требуемое количество изображений, скопируйте их в папку проекта:
res/drawable-hdpi
см. Краткую теорию.
Если имена файлов изображений содержа дефис, то их следует переименовать, например, файл cat-eye.jpg может быть переименован в cat_eye.jpg, т.е. тире заменено на знак подчёркивания. на Android-устройстве с помощью adb в командной строке,
Пример 3.1. Файл res/values/colors.xml
<resources> <color name="greenColor">#FF00FF00</color> <color name="yellowColor">#FFFFFF00</color> </resources>
Пример 3.2. Файл res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyLayout03</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="btn_next">Next</string> <string name="tv_count">Click count = 0</string> <string name="tv_item1">Item 1</string> <string name="tv_item2">Item 2</string> <string name="tv_item3">Item 3</string> <string name="tv_item4">Item 4</string> </resources>
Пример 3.3. Файл res/drawable-hdpi/myborder.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <stroke android:width="2dp" android:color="#00FF00" /> <padding android:bottom="5dp" android:left="4dp" android:right="4dp" android:top="5dp" /> </shape>
Пример 3.4. Файл res/layout/main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/myborder" tools:context=".MainActivity" > <LinearLayout android:id="@+id/rightLayout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/myborder" android:layout_alignParentRight="true" android:orientation="horizontal"> <LinearLayout android:id="@+id/column1Layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/myborder" android:orientation="vertical" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_item1" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_item2" /> </LinearLayout> <LinearLayout android:id="@+id/column2Layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/myborder" android:orientation="vertical" > <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_item3" /> <TextView android:id="@+id/tv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_item4" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/tvCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_count" /> <Button android:id="@+id/btnNext" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="btnNext_Click" android:layout_alignParentBottom="true" android:text="@string/btn_next" /> </RelativeLayout>
Пример 3.5. Файл MainActivity.java
package org.ggpi.MyLayout02; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { public LinearLayout rightLayout; public TextView tvCount; public TextView tv1; public TextView tv2; public TextView tv3; public TextView tv4; int countClick = 0; int numItems = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rightLayout = (LinearLayout)findViewById(R.id.rightLayout); tvCount = (TextView)findViewById(R.id.tvCount); tv1 = (TextView)findViewById(R.id.tv1); tv2 = (TextView)findViewById(R.id.tv2); tv3 = (TextView)findViewById(R.id.tv3); tv4 = (TextView)findViewById(R.id.tv4); tv1.setBackgroundResource(R.color.greenColor); tv2.setBackgroundResource(R.color.greenColor); tv3.setBackgroundResource(R.color.greenColor); tv4.setBackgroundResource(R.color.greenColor); } public void btnNext_Click(View v) { tv1.setBackgroundResource(R.color.greenColor); tv2.setBackgroundResource(R.color.greenColor); tv3.setBackgroundResource(R.color.greenColor); tv4.setBackgroundResource(R.color.greenColor); switch (countClick % numItems) { case 0: { tv1.setBackgroundResource(R.color.yellowColor); break; } case 1: { tv2.setBackgroundResource(R.color.yellowColor); break; } case 2: { tv3.setBackgroundResource(R.color.yellowColor); break; } case 3: { tv4.setBackgroundResource(R.color.yellowColor); break; } } countClick++; tvCount.setText("Click count = " + countClick); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }