1. Trang chủ
  2. » Thể loại khác

custom component trong android

8 80 0

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

THÔNG TIN TÀI LIỆU

Nội dung

http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Custom Component Android Triển khai thành phần riêng thành phần xây dựng sẵn (pre-built) kế thừa từ lớp lớp định nghĩa riêng Android cung cấp widget xây dựng sẵn Button, TextView, EditText, ListView, CheckBox, RadioButton, Gallery, Spinner, AutoCompleteTextView … để bạn sử dụng trực tiếp ứng dụng Android Nhưng có tình bạn khơng thấy hài lòng với tính tồn bất cữ widget có sẵn nào, Android cung cấp cho bạn phương thức để tạo riêng cho bạn Custom Component (Custom Component) để bạn tùy chỉnh cho phù hợp với yêu cầu bạn Nếu bạn cần tạo điều chỉnh nhỏ tới widget layout tồn tại, bạn sử dụng lớp widget layout ghi đè phương thức chúng Chương giải thích cho bạn cách tạo Custom View sử dụng chúng ứng dụng theo bước đơn giản http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     VÍ DỤ CUSTOM COMPONENT TRONG CẤU TRÚC CUSTOM VIEW Tạo Custom Component đơn giản Tạo attribute file với tên attrs.xml thư mục res/values Thay đổi layout file sử dụng Activity thành sau Tạo java file với tên ColorOptionsView package com.vogella.android.customview.compoundview; import com.vogella.android.view.compoundview.R; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; ColorOptionsView extends LinearLayout { public class private View mValue; private ImageView mImage; public ColorOptionsView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ColorOptionsView, 0, 0); String titleText = a.getString(R.styleable.ColorOptionsView_titleText); int valueColor = http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     a.getColor(R.styleable.ColorOptionsView_valueColor,android.R.color.holo_blue_li ght); a.recycle(); setOrientation(LinearLayout.HORIZONTAL); setGravity(Gravity.CENTER_VERTICAL); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_color_options, this, true); = (TextView) getChildAt(0); title.setText(titleText); TextView title mValue = getChildAt(1); mValue.setBackgroundColor(valueColor); mImage = (ImageView) getChildAt(2); } public ColorOptionsView(Context context) { this(context, null); } public void setValueColor(int color) { mValue.setBackgroundColor(color); } public void setImageVisible(boolean visible) { mImage.setVisibility(visible ? View.VISIBLE : View.GONE); } } Sửa đổi Main activity sau chạy ứng dụng package com.vogella.android.customview.compoundview; import com.vogella.android.view.compoundview.R; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClicked(View view) { String text = view.getId() == R.id.view1 ? "Background" : "Foreground"; Toast.LENGTH_SHORT).show(); Toast.makeText(this, text, } } Ứng dụng chạy trông sau: http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Thuyết minh sử dụng code bên lớp Activity Tương tự cách khởi tạo Custom Component, cách mà bạn khởi tạo widget xây dựng sẵn lớp Activity bạn Ví dụ, bạn sử dụng code sau để khởi tạo Custom Component định nghĩa @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DateView dateView = new DateView(this); setContentView(dateView); } Bạn theo dõi kỹ ví dụ để hiểu cách Thuyết minh Custom Component sử dụng code bên Activity Thuyết minh sử dụng Layout XML file Theo cách truyền thống, bạn sử dụng Layout XML file để khởi tạo widget xây dựng sẵn bạn, cách tương tự áp dụng custom widget để bạn khởi tạo Custom Component sử dụng Layout XML file giải thích Ở đây, com.example.compoundview package mà bạn đặt tất code liên quan tới lớp DateView DateView tên lớp Java mà bạn đặt tồn Custom Component android:layout_width="match_parent" android:textColor="#fff" Ở đây, điều quan trọng để ghi nhớ sử dụng tất thuộc tính TextView với Custom Component mà khơng có thay đổi Tương tự, bạn sử dụng tất kiện phương thức với thành phần DateView Custom Component với Custom Attribute Android Chúng ta thấy cách kế thừa tính widget xây dựng sẵn, hai ví dụ cho trên, thấy Custom Component sử dụng tất thuộc tính mặc định lớp cha Nhưng giả sử bạn muốn tạo riêng thuộc tính cho http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     mình, ví dụ thủ tục đơn giản để tạo sử dụng thuộc tính cho Custom Component Android Sau đây, giới thiệu ba thuộc tính sử dụng chúng sau: Bước Bước cho phép sử dụng thuộc tính tùy biến để định nghĩa chúng xml file res/values/ gọi attrs.xml Bạn theo dõi ví dụ attrs.xml sau: Ở đây, name=value muốn sử dụng Layout XML file thuộc tính, format=type kiểu thuộc tính Bước Bước đọc thuộc tính từ Layout XML file thiết lập chúng cho thành phần Trình tự logic tiếp tục cho Constructor mà truyền AttributeSet, nơi chứa thuộc tính XML Để đọc giá trị XML, bạn tạo TypedArray từ AttributeSet, sau sử dụng để đọc thiết lập giá trị sau: TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateView); final int N = a.getIndexCount(); for (int i = 0; i < N; ++i) { int attr = a.getIndex(i); switch (attr) { case R.styleable.DateView_delimiter: String delimiter = a.getString(attr); // something with delimiter R.styleable.DateView_fancyText: break; case boolean fancyText = a.getBoolean(attr, false); // something with fancyText break; } } a.recycle(); Bước Cuối cùng, bạn sử dụng thuộc tính định nghĩa Layout XML file, sau: quan trọng là:xmlns:custom="http://schemas.android.com/apk/res/com.example.compoundview" Ghi nhớ http://schemas.android.com/apk/res/ trì cũ, phần cuối thiết lập thành tên package từ bạn sử dụng thứ đằng sau xmlns: Trong ví dụ này, sử dụng custom, bạn sử dụng tên bạn thích   http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   ... xmlns :android= "http://schemas .android. com/apk/res /android" xmlns:tools="http://schemas .android. com/tools" xmlns :custom= "http://schemas .android. com/apk/res/com.vogella .android. view.compo undview" android: layout_width="match_parent"...

Ngày đăng: 02/12/2017, 07:17

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN