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

Lập Trình Android Lập trình giao diện

20 392 0
Tài liệu đã được kiểm tra trùng lặp

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 0,96 MB

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

Nội dung

Bài tập 5< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="match_parent" android:layout_

Trang 1

Lập Trình Android

Lập trình giao diện

Trần Thái Nam thainam83vn@gmail.com

Anh Le Training

Trang 2

Các widget

Màn hình thiết kế UI trực quan

Lựu chọn: thiết bị, chiều dọc hoặc ngang, Theme, Screen trong project

Thiết kế bằng xml

Trang 3

Bài tập 1: tài liệu P19

Trang 4

Bài tập 2: tài liệu P36

Trang 5

Bài tập 3: tài liệu P41

Trang 6

Bài tập 4: tài liệu P31

Trang 7

Bài tập 5

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

xmlns:tools ="http://schemas.android.com/tools"

android:layout_width ="match_parent"

android:layout_height ="match_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"

tools:context =".MainActivity" >

< ImageView

android:id ="@+id/imageView1"

android:layout_width ="wrap_content"

android:layout_height ="wrap_content"

android:layout_centerHorizontal ="true"

android:layout_centerVertical ="true"

android:src ="@drawable/bottle1" />

< Button

android:id ="@+id/btnChange"

android:layout_width ="wrap_content"

android:layout_height ="wrap_content"

android:layout_alignParentLeft ="true"

android:layout_alignParentTop ="true"

android:text ="Change" />

</ RelativeLayout >

Trang 8

Bài tập 5

protected void onCreate(Bundle savedInstanceState) {

super onCreate(savedInstanceState);

setContentView(R.layout activity_main );

final Iterator<Integer> ite = Arrays.asList(R.drawable bottle1 ,

R.drawable bottle2 , R.drawable bottle3 , R.drawable bottle4 ).iterator();

Drawable pic = getResources().getDrawable(ite.next());

final ImageView imageView1 = (ImageView)findViewById(R.id imageView1 ); imageView1.setImageDrawable(pic);

Button btnChange = (Button)findViewById(R.id btnChange );

btnChange.setOnClickListener( new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

if (ite.hasNext()){

Drawable pic = getResources().getDrawable(ite.next()); imageView1.setImageDrawable(pic);

} }

});

}

Trang 9

Bài tập 6

AutoCompleteTextView

<RelativeLayout

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_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"

tools:context=".MainActivity" >

<AutoCompleteTextView

android:id="@+id/autoCompleteTextView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ems="10" >

<requestFocus />

</AutoCompleteTextView>

</RelativeLayout>

Trang 10

Bài tập 6

protected void onCreate(Bundle savedInstanceState) {

super onCreate(savedInstanceState);

setContentView(R.layout activity_main );

AutoCompleteTextView myAutoComplete;

String item[]={

};

myAutoComplete = (AutoCompleteTextView)findViewById(R.id autoCompleteTextView1 );

myAutoComplete.setAdapter( new ArrayAdapter<String>( this , android.R.layout simple_dropdown_item_1line , item));

}

Trang 11

Bài tập 7: ListView

<RelativeLayout

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_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"

tools:context=".MainActivity" >

<ListView

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true" >

</ListView>

</RelativeLayout>

Trang 12

Bài tập 7: ListView

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final List<String> list = Arrays.asList(

"Android", "iPhone", "WindowsMobile",

"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",

"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",

"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",

"Android", "iPhone", "WindowsMobile");

ListView listView1 = (ListView)findViewById(R.id.listView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(

this, android.R.layout.simple_list_item_1, list);

listView1.setAdapter(adapter);

listView1.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, final View view,

int position, long id) {

final String item = (String) parent.getItemAtPosition(position);

Log.d("ListView", item);

Toast.makeText(MainActivity.this, item, 5000).show();

} });

}

Trang 13

Bài tập 7 bis: GridView

Trang 14

Bài tập 8: Custom ListView

<RelativeLayout

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_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"

tools:context=".MainActivity" >

<ListView

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

Trang 15

Bài tập 8: Custom ListView

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

<LinearLayout

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center_vertical"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:textStyle="bold"

android:textSize="22dp"

android:textColor="#FFFFFF"

android:padding="10dp"

android:text="Weather Photos"

android:background="#336699" />

</LinearLayout>

Trang 16

Bài tập 8: Custom ListView

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

<LinearLayout

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

<ImageView android:id="@+id/imgIcon"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:gravity="center_vertical"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:layout_marginRight="15dp"

android:layout_marginTop="5dp"

android:layout_marginBottom="5dp" />

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center_vertical"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:textStyle="bold"

android:textSize="22dp"

android:textColor="#000000"

android:layout_marginTop="5dp"

android:layout_marginBottom="5dp" />

</LinearLayout>

Trang 17

Bài tập 8: Custom ListView

//Weather.java

public class Weather {

public int icon;

public String title;

public Weather(){

super();

}

public Weather(int icon, String title)

{

super();

this.icon = icon;

this.title = title;

}

}

Trang 18

Bài tập 8

Custom

ListView

//WeatherAdapter.java

public class WeatherAdapter extends ArrayAdapter<Weather>{

Context context;

int layoutResourceId;

Weather data[] = null;

public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {

super(context, layoutResourceId, data);

this.layoutResourceId = layoutResourceId;

this.context = context;

this.data = data;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) { View row = convertView;

WeatherHolder holder = null;

if(row == null) {

LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false);

holder = new WeatherHolder();

holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);

holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

row.setTag(holder);

}

else

{ holder = (WeatherHolder)row.getTag();

}

Weather weather = data[position];

holder.txtTitle.setText(weather.title);

holder.imgIcon.setImageResource(weather.icon);

return row;

}

static class WeatherHolder {

ImageView imgIcon;

TextView txtTitle;

}

Trang 19

Bài tập 8: Custom ListView

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Weather weather_data[] = new Weather[]

{

new Weather(R.drawable.weather_cloudy, "Cloudy"),

new Weather(R.drawable.weather_showers, "Showers"),

new Weather(R.drawable.weather_snow, "Snow"),

new Weather(R.drawable.weather_storm, "Storm"),

new Weather(R.drawable.weather_sunny, "Sunny") };

WeatherAdapter adapter = new WeatherAdapter(this,

R.layout.listview_item_row, weather_data);

ListView listView1 = (ListView)findViewById(R.id.listView1);

View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); listView1.addHeaderView(header);

listView1.setAdapter(adapter);

}

Trang 20

Bài tập 8 bis: Custom

GridView

Ngày đăng: 08/05/2014, 17:55

TỪ KHÓA LIÊN QUAN