android-lecture

android lecture notes

View on GitHub

안드로이드 UI

허준영(jyheo@hansung.ac.kr)

화면 크기를 고려한 디자인

UI요소: Button, EditText, TextView, LinearLayout, …

bg right:25% 80%

UI 요소 크기

UI 요소 크기 - Example

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Width = Match Parent"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Width = wrap content"/>

<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="Width = 100dp"/>

bg right:30% 80%

https://github.com/jyheo/android-lecture-examples/…

레이아웃(Layout)

레이아웃은 사용자 인터페이스에 대한 시각적 구조를 정의합니다. 예컨대 액티비티 또는 앱 위젯에 대한 UI가 이에 해당됩니다. 출처: https://developer.android.com/guide/topics/ui/declaring-layout.html

LinearLayout or RelativeLayout

LinearLayout - Example(1/2)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:orientation="vertical"
        android:background="#aa00ff">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:id="@+id/button1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"/>
    </LinearLayout>

bg right:30% 80%


    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:orientation="horizontal"
        android:background="#00aaff">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 5"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 6"/>
    </LinearLayout>

</LinearLayout>

bg right:30% 80%

https://github.com/jyheo/android-lecture-examples/…

뷰(View)와 ViewGroup

뷰(View)

View/ViewGroup으로 UI 구성

자주 사용되는 View(UI요소들)

TextView와 EditText

Button

CompoundButton

Margin, Padding, Gravity

Margin, padding

Margin, Padding, Gravity Example

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
   android:layout_margin="10dp"
   android:padding="10dp"
    android:background="#aa00ff">
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button"/>
</LinearLayout>

bg right:30% 80%

https://github.com/jyheo/android-lecture-examples/…

Gravity


<FrameLayout/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="Left"
            android:layout_gravity="left"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="center_h"
            android:layout_gravity="center_horizontal"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="right"
            android:layout_gravity="right"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="center_v"
            android:layout_gravity="center_vertical"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="Center"
            android:layout_gravity="center"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="center_v | right"
            android:layout_gravity="center_vertical|right"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="bottom"
            android:layout_gravity="bottom"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="center_h | bottom"
            android:layout_gravity="center_horizontal|bottom"/>
    <Button android:layout_width="100dp" android:layout_height="wrap_content"
            android:text="right | bottom"
            android:layout_gravity="right|bottom"/>
</FrameLayout>

bg right:30% 80%

이벤트 처리

이벤트 리스너(Event Listeners)

Event Listener Interface

Event Listener Example

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Button btn = findViewById(R.id.button1);
       btn.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View view) {
                Toast.makeText(getApplicationContext(),
                        R.string.button_clicked_msg,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

bg right:30% 80%

https://github.com/jyheo/android-lecture-examples/…