wasup
Android) LinearLayout(리니어레이아웃) 본문
반응형
new project : hello03_LinearLayout
수정 : activity_main.xml
수정 : MainActivity.java
추가 : SampleLayoutCodeActivity.java
추가 : AndroidManifest.xml
만들 화면
activity_main.xml
<?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:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
>
<!--
android:orientation="vertical": 위젯을 수직(vertical)으로 배치
android:orientation="horizontal": 위젯을 수평(horizontal)으로 배치
-->
<!--
Button 01 ~ 06
-->
<Button
android:id="@+id/button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="gravity : center_vertical|center_horizontal"
android:textSize="14dp"
android:textStyle="bold"
android:onClick="onButtonClicked7"
/>
<Button
android:id="@+id/button08"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="자바코드로 만들기"
android:textSize="14dp"
android:textStyle="bold"
android:onClick="onButtonClicked8"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="오른쪽"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="중앙"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="왼쪽"
/>
</LinearLayout>
</LinearLayout>
*LinearLayout사이에 View 위젯들을 선언하는 것만으로, 가로 방향 한줄로 나열된 View 위젯들을 확인할 수 있다.
android:layout_gravity="" : 문자열정렬
android:layout_gravity="center_horizontal" : 가운데로
android:layout_marginTop="10dp" : 위쪽마진
android:textStyle="bold" : 텍스트 굵기
android:backgroundTint="@color/teal_200" : 텍스트 배경색
<!--activity_main.xml 버튼01~06-->
<Button
android:id="@+id/button01"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="일반적인 경우"
android:textStyle="bold"
android:backgroundTint="@color/teal_200"
android:onClick="onButtonClicked"
/>
<Button
android:id="@+id/button02"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="padding 사용하기"
android:textSize="16dp"
android:textStyle="bold"
android:onClick="onButtonClicked2"
/>
<Button
android:id="@+id/button03"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="gravity 사용하기"
android:textSize="16dp"
android:textStyle="bold"
android:onClick="onButtonClicked3"
/>
<Button
android:id="@+id/button04"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="weight 사용하기"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/purple_200"
android:background="#111A4C"
android:onClick="onButtonClicked4"
/>
<Button
android:id="@+id/button05"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="baseline 사용하기"
android:textSize="16dp"
android:textStyle="bold"
android:onClick="onButtonClicked5"
/>
<Button
android:id="@+id/button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="gravity : left|top"
android:textSize="14dp"
android:textStyle="bold"
android:onClick="onButtonClicked6"
/>
MainActivity.java
package com.example.hello03_linearlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//method-add
public void onButtonClicked2(View v){
//setContentView(R.layout);
}
//method-add
public void onButtonClicked8(View v){
Intent intent = new Intent(getApplicationContext(),SampleLayoutCodeActivity.class);
startActivity(intent);
}
}
SampleLayoutCodeActivity.java
package com.example.hello03_linearlayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
public class SampleLayoutCodeActivity extends Activity {
//자바코드로 버튼만들기
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainlayout = new LinearLayout(this);//LinearLayout 객체생성
mainlayout.setOrientation(LinearLayout.VERTICAL);//위젯을 수직으로 배치
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//매개변수 MATCH_PARENT 부모랑 맞춤, WRAP_CONTENT 자기자신에 맞춤
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
Button button1 = new Button(this);//버튼생성
button1.setText("button 01");
button1.setLayoutParams(params);
mainlayout.addView(button1);
Button button2 = new Button(this);//버튼생성
button2.setText("button 02");
button2.setLayoutParams(params);
mainlayout.addView(button2);
setContentView(mainlayout);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello03_linearlayout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Hello03_LinearLayout">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SampleLayoutCodeActivity"/>
</application>
</manifest>
반응형
'IT > Android' 카테고리의 다른 글
Android) RelativeLayout(상대적 위치관계 2) (0) | 2021.08.04 |
---|---|
Android) RelativeLayout(상대적 위치관계) (0) | 2021.08.03 |
Android) 버튼 추가해보기 (0) | 2021.08.03 |
Android) 새 화면 추가해보기 (0) | 2021.08.03 |
Android) 텍스트와 버튼 추가해보기 (0) | 2021.08.03 |
Comments