Файл: Работа с ресурсами.docx

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

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

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

Добавлен: 17.03.2024

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

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

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

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



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

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

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

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

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

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

Тема: Работа с ресурсами

Выполнила:

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

Подпись

(Подпись)

Дата

(Дата)

Проверил:

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

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

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

Замечания:



Москва

2022

Загрузка простых типов из ресурсов

Файл arrays.xml




Andrew
Helen
Jack


1999
2002
2010



Файл colors.xml



#FFBB86FC
#FF6200EE
#FF3700B3
#FF03DAC5
#FF018786
#FF000000
#FFFFFFFF
#0000FF
#FF0000


Файл dimens.xml



18pt


Файл drawables.xml



#DDD


Файл strings.xml



Resource Sample
Some Text


Файл activity_main.xml


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:text="String array:"
android:layout_width="wrap_content"
android:paddingRight="5px"
android:textColor="@color/textColor"/>
android:id="@+id/text_strings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:textColor="@color/textColor"/>


android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:text="Int array:"
android:layout_width="wrap_content"
android:paddingRight="20px"
android:padding="5px"
android:textColor="@color/textColor"/>
android:id="@+id/text_digits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColor"/>


android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>



Листинг SimpleValuesActivity.java

package com.example.simplevalues;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class SimpleValuesActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textStrings =
(TextView)findViewById(R.id.text_strings);
String[] names = getResources().getStringArray(R.array.names);
for(int i = 0; i < names.length; i++) {
textStrings.append("Name[" + i + "]: "+ names[i] + "\n");
}
final TextView textDigits =
(TextView)findViewById(R.id.text_digits);
int[] digits = getResources().getIntArray(R.array.digits);
for(int i = 0; i < digits.length; i++) {
textDigits.append("Digit[" + i + "]: "+ digits[i] + "\n");
}
final TextView textStyle = (TextView)findViewById(R.id.text_style);
textStyle.setText(
getResources().getText(R.string.some_text));
textStyle.setTextColor(
getResources().getColor(R.color.textColor));
textStyle.setTextSize(
getResources().getDimension(R.dimen.textPointSize));
textStyle.setBackgroundColor(
getResources().getColor(R.color.backgroundColor));
Window w = this.getWindow();
w.setBackgroundDrawable(
(ColorDrawable)getResources().getDrawable(
R.drawable.grayDrawable));
}
}

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



Загрузка XML-документов

Файл contacts.xml












Файл activity_main.xml


android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:drawSelectorOnTop="false"/>


Файл MainActivity.java

package com.example.xmlresource;

import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.Toast;
import org.xmlpull.v1.XmlPullParser;
import android.app.ListActivity;

import java.util.ArrayList;

public class MainActivity extends ListActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList list = new ArrayList();
try {
XmlPullParser parser = getResources().getXml(R.xml.contacts);
while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) {
if (parser.getEventType()==XmlPullParser.START_TAG &&
parser.getName().equals("contact")) {
list.add(parser.getAttributeValue(0) + "\n" +
parser.getAttributeValue(1) + "\n" +
parser.getAttributeValue(2)); }
parser.next();
}
}
catch (Throwable t) {
Toast.makeText(this, "Error!" +
t.toString(), Toast.LENGTH_LONG).show();
}
setListAdapter(new ArrayAdapter(
this, android.R.layout.simple_list_item_1, list)); } }

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



Определение собственных стилей и тем

Файл styles.xml







Файл activity_main.xml


android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
style="@style/SpecialText"
android:layout_height="wrap_content"
android:text="Hello world!"
android:gravity="center"
android:layout_width="wrap_content"
android:padding="5dp"/>



Файл AndroidManifest.xml


package="com.samples.stylesandthemes">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="StylesAndThemes"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Stars">

android:name=".MainActivity"

android:exported="true">









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