Android studio

android studio LinearLayout

Android studio Layout XML 디자인 부분인 LinearLayout 대해 알아본다.

<LinearLayout 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"
    android:orientation="vertical" # 가로 
    tools:context=".MainActivity">

   <Button
        android:text="버튼"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

android:orientation은 LinearLayout하위에 배치되는 버튼, 텍스트뷰등을 세로 or 가로으로 배치할거냐는 코드이다.

가로 : android:orientation=”vertical”

세로 : android:orientation=”horizontal”

LInearLayout Visibility

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:orientation="vertical"/>

    <Button
        android:id="@+id/LinearLayoutButton"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="LinearLayout OFF" />

</LinearLayout>

Java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class LinearLayout_Activity extends AppCompatActivity {

    LinearLayout linearLayout;
    Button linearLayoutButton;

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

        linearLayout = findViewById(R.id.linearLayout);
        linearLayoutButton = findViewById(R.id.linearLayoutButton);

        linearLayoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int visibilityStatus = linearLayout.getVisibility(); // LinearLayout Visibility 상태확인
                if (visibilityStatus == View.GONE){
                    linearLayoutButton.setText("LinearLayout ON");
                    linearLayout.setVisibility(View.VISIBLE);
                }else if (visibilityStatus == View.VISIBLE){
                    linearLayoutButton.setText("LinearLayout OFF");
                    linearLayout.setVisibility(View.GONE);
                }
            }
        });
    }
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다