반응형
메인 엑티비티
package grocery.gohool.slidemenu;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.Drawer);
//슬라이드메뉴
view = (View) findViewById(R.id.slider);
Button openBtn = (Button) findViewById(R.id.openBtn);
Button closeBtn = (Button) findViewById(R.id.closeBtn);
openBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(view);
}
});
closeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
}
});
}
}
메인엑티비티 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/Drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/openBtn"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="메뉴"
android:textStyle="bold"
android:textSize="20dp"
app:backgroundTint="#A33434" />
</LinearLayout>
<include layout="@layout/slider"/>
</androidx.drawerlayout.widget.DrawerLayout>
슬라이더 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"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@color/teal_700"
android:id="@+id/slider"
android:layout_gravity="start"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴닫기"
app:backgroundTint="#B73939" />
</LinearLayout>
주의할점
1.자식이 layout_gracity를 가진다는 점.
2.include를 통해 부모에게 슬라이더를 넣어줘야한다는 점.
내일은 슬라이드메뉴와 함께 하단메뉴(Fragment)를 같이 만들어보려고한다.
반응형
'개발언어 > JAVA' 카테고리의 다른 글
안드로이드 Fragment (0) | 2020.12.25 |
---|---|
안드로이드 생명주기 (0) | 2020.12.25 |
안드로이드 다중 체크박스 결과 값 보내기 (0) | 2020.12.24 |
안드로이드 앱(todolist 만들기) (0) | 2020.12.19 |
Shared Preferences(안드로이드 데이터 저장(키, 값)) (0) | 2020.12.19 |