Файл: Меню. Локализация ресурсов. Работа активами.docx

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 17.03.2024

Просмотров: 7

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ АВТОНОМНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ



МОСКОВСКИЙ ПОЛИТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ

Кафедра Информатики и информационных технологий

направление подготовки

09.03.02 «Информационные системы и технологии»

ЛАБОРАТОРНАЯ РАБОТА №9

Дисциплина: Программирование для мобильных устройств

Тема: Меню. Локализация ресурсов. Работа активами.

Выполнила:

студент группы 191-723 Шаповалюк А.О.

Подпись

(Подпись)

Дата

(Дата)

Проверил:

(Фамилия И.О., степень, звание) (Оценка)

Дата, подпись

(Дата) (Подпись)

Замечания:



Москва

2022

Подменю и меню с переключателями

Файл menu.xml




android:title="@string/color">

android:title="@string/blue"/>
android:title="@string/red"/>
android:title="@string/green"/>


android:title="@string/exit"/>

Файл strings.xml


Menu
Press Menu
Red
Green
Blue
Color
Exit


Файл activity_main.xml


android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
android:orientation="vertical"
android:gravity="center">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:gravity="center"
android:textStyle="bold"/>



Листинг MainActivity.java

package com.example.menu;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Загрузка меню из XML-файла
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final LinearLayout mylayout = (LinearLayout)findViewById(R.id.root);
switch (item.getItemId()) {
case R.id.red:
mylayout.setBackgroundColor(Color.parseColor("#FF0000"));
return true;
case R.id.green:
mylayout.setBackgroundColor(Color.parseColor("#00FF00"));
return true;
case R.id.blue:
mylayout.setBackgroundColor(Color.parseColor("#0000FF"));
return true;
case R.id.exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

Скриншот выполнения программы:



Создание локализованных ресурсов

Файл strings.xml


Меню
Нажмите Меню
Красный
Зелёный
Синий
Цвет
Выход


Скриншот выполнения программы:





Работа со сторонними шрифтами

Файл MainActivity.xml

package com.example.fonts;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView text1 = (TextView)findViewById(R.id.text1);
text1.setTypeface(Typeface.createFromAsset(
getAssets(), "fonts/aquarelle.ttf"));
final TextView text2 = (TextView)findViewById(R.id.text2);
text2.setTypeface(Typeface.createFromAsset(
getAssets(), "fonts/cactus.ttf"));
final TextView text3 = (TextView)findViewById(R.id.text3);
text3.setTypeface(Typeface.createFromAsset(
getAssets(), "fonts/egypt.ttf"));
final TextView text4 = (TextView)findViewById(R.id.text4);
text4.setTypeface(Typeface.createFromAsset(
getAssets(), "fonts/rurintania.ttf"));
final TextView text5 = (TextView)findViewById(R.id.text5);
text4.setTypeface(Typeface.createFromAsset(
getAssets(), "fonts/tron.ttf"));
}
}

Файл activity_mail.xml


xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
android:text="Aquarelle"
android:textColor="@android:color/holo_red_light"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="36sp"
android:padding="10sp"/>
android:text="Cactus"
android:textColor="@android:color/holo_green_light"
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:padding="10sp"/>
android:text="Egypt"
android:textColor="@android:color/holo_orange_light"
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:padding="10sp"/>
android:text="Rurintania"
android:textColor="@android:color/holo_purple"
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:padding="10sp"/>
android:text="Tron"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:padding="10sp"/>


Скриншот выполнения программы: