Linear Layout

Android 2011. 6. 9. 15:13

Linear Layout

LinearLayout is a ViewGroup that displays child View elements in a linear direction, either vertically or horizontally.

지나친 LinearLayout사용에 주의를 할 필요가 있습니다.중첩된 여러 LinearLayout들을 사용하려면, RelativeLayout 사용을 고려할 수 있습니다.

  1. HelloLinearLayout.라는 이름의 새로운 프로젝트를 생성합니다.
  2. res/layout/main.xml 를 열어 삽입합니다.
    <?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">

     
    <LinearLayout
         
    android:orientation="horizontal"
         
    android:layout_width="fill_parent"
         
    android:layout_height="fill_parent"
         
    android:layout_weight="1">
         
    <TextView
             
    android:text="red"
             
    android:gravity="center_horizontal"
             
    android:background="#aa0000"
             
    android:layout_width="wrap_content"
             
    android:layout_height="fill_parent"
             
    android:layout_weight="1"/>
         
    <TextView
             
    android:text="green"
             
    android:gravity="center_horizontal"
             
    android:background="#00aa00"
             
    android:layout_width="wrap_content"
             
    android:layout_height="fill_parent"
             
    android:layout_weight="1"/>
         
    <TextView
             
    android:text="blue"
             
    android:gravity="center_horizontal"
             
    android:background="#0000aa"
             
    android:layout_width="wrap_content"
             
    android:layout_height="fill_parent"
             
    android:layout_weight="1"/>
         
    <TextView
             
    android:text="yellow"
             
    android:gravity="center_horizontal"
             
    android:background="#aaaa00"
             
    android:layout_width="wrap_content"
             
    android:layout_height="fill_parent"
             
    android:layout_weight="1"/>
     
    </LinearLayout>
           
     
    <LinearLayout
       
    android:orientation="vertical"
       
    android:layout_width="fill_parent"
       
    android:layout_height="fill_parent"
       
    android:layout_weight="1">
       
    <TextView
           
    android:text="row one"
           
    android:textSize="15pt"
           
    android:layout_width="fill_parent"
           
    android:layout_height="wrap_content"
           
    android:layout_weight="1"/>
       
    <TextView
           
    android:text="row two"
           
    android:textSize="15pt"
           
    android:layout_width="fill_parent"
           
    android:layout_height="wrap_content"
           
    android:layout_weight="1"/>
       
    <TextView
           
    android:text="row three"
           
    android:textSize="15pt"
           
    android:layout_width="fill_parent"
           
    android:layout_height="wrap_content"
           
    android:layout_weight="1"/>
       
    <TextView
           
    android:text="row four"
           
    android:textSize="15pt"
           
    android:layout_width="fill_parent"
           
    android:layout_height="wrap_content"
           
    android:layout_weight="1"/>
     
    </LinearLayout>

    </LinearLayout>

    신중하게 XML을 확인하십시요. LinearLayout 는 최상위 입니다.that defines its orientation to be vertical—all child Views (of which it has two) will be stacked vertically. The first child is another LinearLayout that uses a horizontal orientation and the second child is a LinearLayout that uses a vertical orientation. Each of these nested LinearLayouts contain several TextView elements, which are oriented with each other in the manner defined by their parent LinearLayout.

  3. Now open HelloLinearLayout.java and be sure it loads the res/layout/main.xml layout in the onCreate() method:

    public void onCreate(Bundle savedInstanceState) {
       
    super.onCreate(savedInstanceState);
        setContentView
    (R.layout.main);
    }

    The setContentView(int) method loads the layout file for the Activity, specified by the resource ID — R.layout.main refers to the res/layout/main.xml layout file.

  4. Run the application.

You should see the following:

Notice how the XML attributes define each View's behavior. Try experimenting with different values for android:layout_weight to see how the screen real estate is distributed based on the weight of each element. See the Common Layout Objects document for more about how LinearLayout handles the android:layout_weight attribute.

References

↑ Go to top

← Back to Hello, Views

'Android' 카테고리의 다른 글

WebView  (0) 2011.06.09
안드로이드 스터디 시작!  (0) 2011.06.09
Posted by 다엘
,

WebView

Android 2011. 6. 9. 14:58
http://developer.android.com/reference/android/webkit/WebView.html


WebView는 웹킷 엔진을 이용하여 View에 웹페이지를 보여준다.
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AbsoluteLayout
         ↳ android.webkit.WebView

 웹킷엔진을 이용하려면  INTERNET permission이 필요함.

<uses-permission android:name="android.permission.INTERNET" />
 manifest파일에 위 코드 추가.


 <WebView

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
/>


layout에 위 코드 추가 

엑티비티 소스 코드

package com.daelity.WebViewTest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class mainActivity extends Activity {
WebView browser; //WebView
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        browser = (WebView) findViewById(R.id.webView);
        browser.loadUrl("http://m.naver.com");//Url을 로드
        browser.getSettings().setJavaScriptEnabled(true);//자바스크립트 사용
    }

'Android' 카테고리의 다른 글

Linear Layout  (0) 2011.06.09
안드로이드 스터디 시작!  (0) 2011.06.09
Posted by 다엘
,