20.11.2016 Views

ANDROID APP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

จากเลย์เอาต์ในชุดคำสั่งที่แล้ว เราเขียนด้วยภาษาจาวาเหมือนในชุดคำสั่งที่ 4.4 การสร้าง<br />

เลย์เอาต์เช่นนี้จะง่ายต่อการสร้าง แต่จะขาดคุณสมบัติในแง่การใช้งานแบบโปรแกรมมิ่ง ซึ่งจะเป็น<br />

ประโยชน์ในกรณีที่มีการใช้งานเลย์เอาต์ร่วมกันในหลายๆ แอพ โดยที่ก่อนหน้านี้เราได้ทำการแยกส่วน<br />

ของเลย์เอาต์และชุดคำสั่งออกจากกันไว้แล้ว<br />

ชุดคำสั่งที่ 4.4 src/com/cookbook/programmaticlayout/ProgrammaticLayout.java<br />

package com.cookbook.programmatic_layout;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.view.ViewGroup;<br />

import android.view.ViewGroup.LayoutParams;<br />

import android.widget.RelativeLayout;<br />

import android.widget.TextView;<br />

public class ProgrammaticLayout extends Activity {<br />

private int TEXTVIEW1_ID = 100011;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

วิวและกลุ่มของวิว<br />

//Here is an alternative to: setContentView(R.layout.main);<br />

final RelativeLayout relLayout = new RelativeLayout( this );<br />

relLayout.setLayoutParams( new RelativeLayout.LayoutParams(<br />

LayoutParams.FILL_PARENT,<br />

LayoutParams.FILL_PARENT ) );<br />

TextView textView1 = new TextView( this );<br />

textView1.setText("middle");<br />

textView1.setTag(TEXTVIEW1_ID);<br />

RelativeLayout.LayoutParams text1layout = new<br />

RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,<br />

LayoutParams.WRAP_CONTENT );<br />

text1layout.addRule( RelativeLayout.CENTER_IN_PARENT );<br />

relLayout.addView(textView1, text1layout);<br />

TextView textView2 = new TextView( this );<br />

textView2.setText("high");<br />

RelativeLayout.LayoutParams text2Layout = new<br />

RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,<br />

LayoutParams.WRAP_CONTENT );<br />

text2Layout.addRule(RelativeLayout.ABOVE, TEXTVIEW1_ID );<br />

relLayout.addView( textView2, text2Layout );<br />

91<br />

}<br />

}<br />

setContentView( relLayout );

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!