-
[안드로이드 스튜디오] 버튼 배치프로그래밍/XML | JAVA 2024. 3. 18. 15:34
<?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" tools:context=".MainActivity" android:gravity="center"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼1"/> </LinearLayout>
package com.example.a04_12; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
가중치
<?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="horizontal" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:layout_gravity="center" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:layout_gravity="center" android:text="Button3" /> </LinearLayout>
package com.example.a04_12; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
에디트 텍스트만 구중치가 1이고 나머지는 전부 0
<?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"> <EditText android:id="@+id/editTextText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="To" /> <!-- 디폴트 가중치는 0 --> <EditText android:id="@+id/editTextText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Subject" /> <EditText android:id="@+id/editTextText3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="Message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Send" /> </LinearLayout>
package com.example.a04_19; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
테이블 레이아웃
상대적 레이아웃
<?xml version="1.0" encoding="utf-8"?> <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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClicked" android:text="COLOR 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClicked" android:text="COLOR 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClicked" android:text="COLOR 3" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#3F51B5" /> <TextView android:id="@+id/view2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000"/> <TextView android:id="@+id/view3" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF0F"/> </FrameLayout> </androidx.appcompat.widget.LinearLayoutCompat>
package com.example.layouttest; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { TextView tv1, tv2, tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.view1); tv2 = (TextView) findViewById(R.id.view2); tv3 = (TextView) findViewById(R.id.view3); } public void onClicked(View view) { tv1.setVisibility(View.INVISIBLE); tv2.setVisibility(View.INVISIBLE); tv3.setVisibility(View.INVISIBLE); if(view.getId() == R.id.button1) { tv1.setVisibility(View.VISIBLE); } else if(view.getId() == R.id.button2) { tv2.setVisibility(View.VISIBLE); } else if(view.getId() == R.id.button3) { tv3.setVisibility(View.VISIBLE); } } }
'프로그래밍 > XML | JAVA' 카테고리의 다른 글
[안드로이드 스튜디오] 고급 위젯과 이벤트 처리 (0) 2024.03.26 [안드로이드 스튜디오] 계산기 UI (0) 2024.03.18 [안드로이드 스튜디오] 1~100까지 랜덤게임 맞추기 (0) 2024.03.12 [안드로이드 스튜디오] 주사위 굴리기 (0) 2024.03.12 [안드로이드 스튜디오] 버튼을 눌러 EditText에 있는 값 출력하기 (0) 2024.03.12