1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Android: Tổng hợp Style pot

4 351 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 111,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trong bài viết này mình sẽ demo 1 số cách chỉnh định dạng chữ cho các control như TextView, EditText hay button.. Các bạn tạo Project như sau: Project name: DemoStyle Build Target: Andro

Trang 1

Trong bài viết này mình sẽ demo 1 số cách chỉnh định dạng chữ cho các control như TextView, EditText hay button.

Các bạn tạo Project như sau:

Project name: DemoStyle

Build Target: Android 2.3.3

Application name: DemoStyle

Package name: com.dac.DemoStyle

Create Activity: StylesActivity

Tiếp theo các bạn rename lại file main.xml thành styles.xml và thiết kế như sau:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent" android:layout_height="fill_parent"

>

<TextView android:id="@+id/tv"

android:layout_width="fill_parent" android:layout_height="wrap_content" />

<TextView android:id="@+id/tvStyled"

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:text="@+string/styledText"

/>

<TextView android:id="@+id/tv3"

android:layout_width="fill_parent" android:layout_height="wrap_content" />

<EditText android:id="@+id/et"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text|textAutoCorrect|textAutoComplete|textMultiLine"

/>

<Button android:id="@+id/button1"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/italics"

/>

<TextView android:id="@+id/errors"

style="@style/ErrorText"

android:text="There's trouble down at the mill."

/>

<TextView android:id="@+id/danger"

style="@style/ErrorText.Danger"

Trang 2

android:text="SERIOUSLY! THERE'S TROUBLE DOWN AT THE MILL!!"

/>

</LinearLayout>

Tiếp theo các bạn thêm vào 1 file styles.xml và viết code như sau:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="ErrorText">

<item name="android:layout_width">fill_parent</item>

<item name="android:layout_height">wrap_content</item>

<item name="android:textColor">#FF0000</item>

<item name="android:typeface">monospace</item>

</style>

<style name="ErrorText.Danger" >

<item name="android:textStyle">bold</item>

</style>

</resources>

Các bạn thêm vào file strings.xml như sau:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="styles_name">Styles Demo</string>

<string name="textview_name">TextView Demo</string>

<string name="styledText"><i Static</i style in a < >TextView</b Here\'s

<strike>strike-through</strike>

and here\'s <sub>subscript</sub>, four squared: 4<sup> </sup> How about

<big>BIG</big>, <small>small</small>

and <tt>monospace</tt>? And finally, what about <small><sub>small

subscripts</sub></small> </string>

<string name="italics"><i Italics</i></string>

Và trong fileStylesActivity.java các bạn code như sau:

package com.dac.DemoStyle

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.text.Spannable;

import android.text.style.BackgroundColorSpan;

import android.text.style.StyleSpan;

import android.text.util.Linkify;

import android.widget.EditText;

import android.widget.TextView;

Trang 3

public class StylesActivity extends Activity {

/** Called when the activity is first created */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.styles);

TextView tv =(TextView)this.findViewById(R.id.tv);

tv.setAutoLinkMask(Linkify.ALL);

tv.setText("Nobilomo_1990@yahoo.com");

TextView tv3 =(TextView)this.findViewById(R.id.tv3);

tv3.setText("Styling the content of a TextView dynamically", TextView.BufferType.SPANNABLE);

Spannable spn = (Spannable) tv3.getText();

spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),

0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

EditText et =(EditText)this.findViewById(R.id.et);

et.setText("Styling the content of an EditText dynamically"); Spannable spn2 = (Spannable) et.getText();

spn2.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

spn2.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),

0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

Hình ảnh sau khi demo ứng dụng như sau:

Trang 4

Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết về forum trang web

www.laptrinhdidong.vn Rất mong nhận được sự phản hồi của các bạn

Ngày đăng: 08/08/2014, 07:20

TỪ KHÓA LIÊN QUAN

w