일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- javascript
- php
- mariadb
- H2O
- 한글
- it
- 번역
- 안드로이드
- 컴퓨터과학총론
- 자바
- MySQL
- java
- 구글
- 개발자
- android
- centOS7
- unity
- caddy
- 해석
- error
- ubuntu
- techEmpower
- Portfolio
- kakao
- 개발
- server
- NGINX
- C lanuage
- 프래그먼트
- C
- Today
- Total
개발모음집
[안드로이드 프로그래밍] Day05 ~ Day06 본문
뷰와 뷰그룹
뷰를 담고 있는게 뷰그룹, 뷰그룹이 뷰들을 배치하는 역할도 할 수 있다, 그리고 뷰의 배치 방식을 정의한 것이 레이아웃,
배치하기 위한 속성들
속성
채우기: fill model -> 뷰를 부모뷰의 여유 공간에 어떻게 채울 것인지를 설정합니다.
방향: orientation -> 뷰를 추가하는 방향을 설정합니다.(리니어 레이아웃의 경우에만)
정렬 방향: gravity -> 뷰의 정렬 방향을 설정합니다.
여유 공간: padding -> 뷰의 여유공간을 설정합니다.
공간가중치: weight -> 뷰가 차지하는 공간의 가중치 값을 설정합니다.
* 안드로이드 레이아웃
리니어 레이아웃: 박스 모델, 사각형 영역들을 이용해 화면을 구성하는 방법, 표준 자바의 BoxLayout과 유사
상대 레이아웃: 규칙 기반 모델, 부모 컨테이너나 다른 뷰와의 상대적 위치를 이용해 화면을 구성하는 방법
MyFrameLayout의 activity_main.xml파일
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView"
android:background="#ff662211"
android:layout_below="@id/button"
android:layout_above="@id/button2"
/> /* 윗, 아래버튼을 제외하고 채우고 싶다 */
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
프레임 레이아웃: 기본 단위 모델, 하나의 뷰만 보여주는 방법, 가장 단순하지만 여러 개의 추가하는 경우 중첩시킬 수 있으므로 뷰를 중첩한 후 각 뷰를 전환하여 보여주는 방식으로 사용할 때 유용함
MyFrameLayout의 activity_main.xml파일
<?xml version="1.0" encoding="utf-8"?>
<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="org.androidtown.myframelayout.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이미지 바꾸기"
android:id="@+id/button"
android:textColor="#ff0000ff"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/dream01"
android:layout_gravity="center" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/dream02" />
</FrameLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignBottom="@+id/button"
android:layout_alignRight="@+id/frameLayout"
android:layout_alignEnd="@+id/frameLayout"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:textColor="#ff00ff00"
android:hint="비밀번호입력"
android:inputType="number|textPassword" />
</RelativeLayout>
MyFrameLayout의 MainActivity.java파일
package org.androidtown.myframelayout;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView, imageView2;
boolean selected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 두 개의 사진의 이미지를 찾는다.
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(selected) {
imageView.setVisibility(View.VISIBLE);
imageView2.setVisibility(View.GONE); // GONE = invisible
}else {
imageView.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}
selected = !selected; // true와 false를 실행시마다 바꿔줌
}
});
}
}
xml의 디자인탭 - 속성 - inputType에서 비밀번호의 특성들을 살릴 수 있다.
실제 파일을 복사하여 res/drawable에 붙여넣기하면 실제로 복사가 된다.
테이블 레이아웃: 격자 모델, 격자 모양의 배열을 이용하여 화면을 구성하는 방법
스크롤 뷰: 스크롤이 가능한 컨테이너, 뷰 또는 뷰그룹이 들어갈 수 있으며 화면 영역을 넘어갈 때 스크롤 기능 제공
* 안드로이드에서 기본적으로 제공하는 레이아웃들을 사용할 때는
항상 android:layout_width와 android:layout_height 속성이 들어가야한다.
* xmlns:android -> API에서 정의한 요소들을 사용한다는 의미
XML 레이아웃 파일의 가장 위쪽에 xmlns:android속성이 들어간다.
이 속성은 한 파일에 한 번만 들어가면 되는 속성, 하나의 XML레이아웃파일에 한 번씩 들어간다고 생각
하면 된다.
* 새로운 프로젝트를 생성하면 default layout value는 ‘RelativeLayout’이다.
그래서 가장 바깥에 있는 태그를 'LinearLayout'으로 바꿔야한다.
* 앱이 실행될 때 처음 보이는 액티비티 변경법
AndroidManifest.xml 파일 - activity태그의 android:name 속성값을 바꾸면 지정한 액티비티가 뜬다.
<activity android:name="LayoutGravityActivity"></activity>
* setContentView(R.layout.activity_main);
R: res폴더, layout: layout폴더, activity_main: activity_main.xml
xml파일을 복사하여 보여주겠다는 의미
'Android' 카테고리의 다른 글
[안드로이드 프로그래밍] Day11 (0) | 2016.06.08 |
---|---|
[안드로이드 프로그래밍] Day09 (0) | 2016.06.07 |
[안드로이드 프로그래밍] Day07 ~ Day08 인플레이션 ~ 인텐트, 부가데이터 (0) | 2016.06.05 |
[안드로이드 프로그래밍] Day04 (0) | 2016.06.03 |
[안드로이드 프로그래밍] Day01 ~ Day02 (0) | 2016.06.03 |