LinearLayout
각 객체를 수직 혹은 수평방향으로 배열하는 레이아웃
가장 활용 빈도가 높다!
실전으로 가기 전
LinearLayout 과 LinearLayoutCompat의 차이
API레벨과 무관하게 사용 시 LinearLayoutCompat 사용가능함
LinearLayout은 targetSdk에 해당하는 API레벨 메소드를 지원한다.
==>LinearLayoutCompat 은 구 버전 API에서도 사용할 수 있다. 그럼 이걸 사용해야 겠군!!
크게
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" ->요렇게 정렬 지정 (디폴트는 horizontal)
></androidx.appcompat.widget.LinearLayoutCompat>
요 태크로 감싸줌! 요거가 걍 기본 셋팅인 듯 보임
gravity는 레이아웃에 layout_gravity는 위젯에 보통 지정
orientation이
vertical인 경우는 right,left가 적용안됨.즉 top,bottom가 적용됨
horizontal인 경우 top,bottomㅣ 적용안됨. 즉 right,left가 적용됨
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffff00"
>
<!--
gravity는 해당 뷰의 컨텐츠정렬
layout_gravity는 해당 뷰가 배치된 레이아웃(부모)을 기준으로 정렬
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼1"
android:layout_gravity="right"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼2"
android:gravity="right|center_vertical"
/>
</LinearLayout>
layout_gravity는 부모기준으로 정렬!
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#ff0000">
<!--수직방향 정렬은 적용이 안됨,
(레이아웃의 orientation이 vertical)
아래 영역은 다른 컴포넌트가 배치 될 영역이므로 침범을 할 수 없다.
레이아웃이 vertical : 레이아웃 기준 세로정렬이 안됨
레이아웃이 horizontal : 레이아웃 기준 가로정렬이 안됨
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼1"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼2"
android:layout_gravity="end"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
android:layout_weight(가중치)
===> 비율이네!! 여백없이 꽉 채울 수 있게 해준다
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:baselineAligned="true" >
<!--android:baselineAligned 디폴트가 true 니까 신경 안 써도 됨 !! -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="텍스트뷰1"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="텍스트뷰2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="텍스트뷰3"
android:textSize="45sp"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff"
android:padding="5dp"
>
<!-- weigth를 적용 시킬 때 설정!!
orentation이 vertical 일 때 : layout_height="0dp"
horizontal 일 때 : layout_width="0dp"
-->
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="버튼1"
/>
<Button
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="버튼2"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="버튼3"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
<!--나머지는 밑에가 다 차지-->
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9bc571"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:text="버튼1"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:text="버튼2"
android:layout_gravity="center"
/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:text="버튼3"
android:layout_gravity="center"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
2.LinearLayout 02 - 자바코드로 만들기
layout 폴더에 activity main 파일을 지워버려 헐....
그럼 작업은
java폴더에
MainActivity.java 에서 한다....
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("리니어레이아웃 - 자바코드");
//레이아웃 용 XML파일 불 필요. 아래코드를 주석
// setContentView(R.layout.activity_main);
//1.linearlayout객체 생성(이게 태그같은 역할)
LinearLayoutCompat layoutCompat = new LinearLayoutCompat(this);
//2.오리엔테이션 설정
layoutCompat.setOrientation(LinearLayoutCompat.HORIZONTAL);
//3.리니어 레이아웃의 가로폭/세로폭 설정
LinearLayoutCompat.LayoutParams layoutParams=
new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT,LinearLayoutCompat.LayoutParams.MATCH_PARENT);
//3-1LayoutParams를 LinearLayout객체에 적용
layoutCompat.setLayoutParams(layoutParams);
//3-2배경색
layoutCompat.setBackgroundColor(Color.rgb(255,255,0));
//4.button타입 선언
Button btnOne ,btnTwo;
//5.button타입 객체생성
btnOne = new Button(this);
btnTwo = new Button(this);
//6.버튼들의 속성 설정
LinearLayoutCompat.LayoutParams btnLayoutParams=
new LinearLayoutCompat.LayoutParams(0,LinearLayoutCompat.LayoutParams.WRAP_CONTENT,1);
btnOne.setLayoutParams(btnLayoutParams);
btnOne.setText("버튼1");
btnOne.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
btnOne.setTextColor(Color.RED);
//※버튼에 가로/세로폭 미 적용시
//디폴트가 가로폭 ,세로폭은 WRAP_CONTENT임.
btnTwo.setLayoutParams(btnLayoutParams);
btnTwo.setText("버튼2");
//7.레이아웃에 버튼 부착:addView()
layoutCompat.addView(btnOne);
layoutCompat.addView(btnTwo);
//8.이벤트부착
btnOne.setOnClickListener(view -> Toast.makeText(view.getContext(),"첫 번재 버튼입니다.",Toast.LENGTH_SHORT).show());
btnTwo.setOnClickListener(view -> Toast.makeText(getApplicationContext(),"두 번재 버튼입니다.",Toast.LENGTH_SHORT).show());
///9 레이아웃 전개
setContentView(layoutCompat);
}
}
자바코드가.... 더 복잡한 것 같음.....
'학원 > ANDROID' 카테고리의 다른 글
01/05 77-2 [Android] GridLayout /LayoutExam (1) | 2023.01.07 |
---|---|
01/05 77-1 [Android] FrameLayout (1) | 2023.01.07 |
01/04 76-4 [Android] TableLayout (0) | 2023.01.04 |
01/04 76-3 [Android] RelativeLayout (0) | 2023.01.04 |
01/04 76-1 [Android] 환경설정/개념설명 (1) | 2023.01.04 |