Difference between revisions of "Android Studio: Layout Layar"
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 45: | Line 45: | ||
</LinearLayout> | </LinearLayout> | ||
− | + | Setelah anda menyatakan layout anda dalam XML, simpan file dengan ekstensi .xml, di res / layout / direktori proyek Android anda, sehingga akan dicompilasi dengan benar. | |
More information about the syntax for a layout XML file is available in the Layout Resources document. | More information about the syntax for a layout XML file is available in the Layout Resources document. | ||
Line 107: | Line 107: | ||
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible). | An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible). | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Referensi== | ==Referensi== | ||
* https://developer.android.com/guide/topics/ui/declaring-layout.html | * https://developer.android.com/guide/topics/ui/declaring-layout.html |
Revision as of 13:48, 7 May 2015
Sumber: https://developer.android.com/guide/topics/ui/declaring-layout.html
Layout mendefinisikan struktur virtual untuk interface user, seperti UI untuk activity atau app widget. Kita dapat mendeklarasikan layout dengan dua cara:
- Deklarasikan elemen UI elements di XML. Android memberikan vocabulary XML yang straightforward XML yang berhubungan dengan class View dan subclass, seperti untuk widget dan layout.
- Inisiasi layout elemen saat runtime. Aplikasi kita dapat menggunakan program membuat objek View dan ViewGroup (dan memanipulasi property-nya).
Framework Android memberikan kita flexibilitas untuk menggunakan ke dua metoda tersebut untuk mendeklarasikan dan memanaje UI aplikasi kita. Contoh, kita dapat mendeklarasikan default layout aplikasi kita di XML, termasuk elemen screen yang akan muncul di dalamnya termasuk property-nya. Kemudian, kita dapat menambahkan code di aplikasi kita yang dapat memodifikasi kondisi screen object, termasuk deklarasi di XML sambil berjalan.
The ADT Plugin for Eclipse offers a layout preview of your XML — with the XML file opened, select the Layout tab. You should also try the Hierarchy Viewer tool, for debugging layouts — it reveals layout property values, draws wireframes with padding/margin indicators, and full rendered views while you debug on the emulator or device. The layoutopt tool lets you quickly analyze your layouts and hierarchies for inefficiencies or other problems.
Keuntungan untuk men-deklarasikan UI anda dalam XML adalah bahwa hal itu memungkinkan anda untuk lebih memisahkan penyajian aplikasi anda dari kode yang mengontrol perilakunya. Deskripsi UI anda menjadi eksternal dari kode aplikasi anda, yang berarti bahwa anda dapat mengubah atau beradaptasi tanpa harus memodifikasi kode sumber dan mengkompilasi ulang. Misalnya, anda dapat membuat layout XML untuk orientasi yang berbeda layar, ukuran layar perangkat yang berbeda, dan bahasa yang berbeda. Selain itu, menyatakan tata letak di XML membuatnya lebih mudah untuk memvisualisasikan struktur UI anda, sehingga lebih mudah untuk masalah debug. Dengan demikian, dokumen ini berfokus pada mengajar anda bagaimana untuk menyatakan tata letak Anda dalam XML.
Secara umum, vocabulary XML untuk menyatakan elemen UI erat mengikuti struktur dan penamaan kelas dan metode, di mana nama elemen sesuai dengan nama kelas dan atribut nama sesuai dengan metode. Bahkan, korespondensi sering begitu langsung yang anda bisa menebak atribut XML tertentu sesuai dengan metode kelas, atau menebak kelas yang sesuai dengan elemen XML yang diberikan. Namun, perlu diketahui bahwa tidak semua vocabulary identik. Dalam beberapa kasus, ada perbedaan penamaan sedikit. Misalnya, unsur EditText memiliki atribut teks yang sesuai dengan EditText.setText ().
Tip: Learn more about different layout types in Common Layout Objects. There are also a collection of tutorials on building various layouts in the Hello Views tutorial guide
Menulis XML
Menggunakan vocabulary Android XML , anda dapat dengan cepat merancang layout UI dan elemen layar yang dikandungnya, dengan cara yang sama anda membuat halaman web di HTML - dengan serangkaian elemen nested.
Setiap file layout harus berisi hanya satu elemen root, yang harus sebuah View atau objek ViewGroup. Setelah anda mendefinisikan elemen root, anda dapat menambahkan objek layout tambahan atau widget sebagai elemen anak untuk secara bertahap membangun hirarki View yang mendefinisikan layout anda. Sebagai contoh, berikut adalah layout XML yang menggunakan LinearLayout vertikal untuk menampung TextView dan Button:
<?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/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout>
Setelah anda menyatakan layout anda dalam XML, simpan file dengan ekstensi .xml, di res / layout / direktori proyek Android anda, sehingga akan dicompilasi dengan benar.
More information about the syntax for a layout XML file is available in the Layout Resources document.
Load the XML Resource
When you compile your application, each XML layout file is compiled into a View resource. You should load the layout resource from your application code, in your Activity.onCreate() callback implementation. Do so by calling setContentView(), passing it the reference to your layout resource in the form of: R.layout.layout_file_name. For example, if your XML layout is saved as main_layout.xml, you would load it for your Activity like so:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); }
The onCreate() callback method in your Activity is called by the Android framework when your Activity is launched (see the discussion about lifecycles, in the Activities document).
Attributes
Every View and ViewGroup object supports their own variety of XML attributes. Some attributes are specific to a View object (for example, TextView supports the textSize attribute), but these attributes are also inherited by any View objects that may extend this class. Some are common to all View objects, because they are inherited from the root View class (like the id attribute). And, other attributes are considered "layout parameters," which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object. ID
Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute. This is an XML attribute common to all View objects (defined by the View class) and you will use it very often. The syntax for an ID, inside an XML tag is:
android:id="@+id/my_button"
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="@android:id/empty"
With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.
In order to create views and reference them from the application, a common pattern is to:
Define a view/widget in the layout file and assign it a unique ID:
<Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/my_button_text"/>
Then create an instance of the view object and capture it from the layout (typically in the onCreate() method):
Button myButton = (Button) findViewById(R.id.my_button);
Defining IDs for view objects is important when creating a RelativeLayout. In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).