250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 입문
- Node js
- es6
- java
- javascript
- 자바스크립트
- 안드로이드
- mongodb
- 안드로이드 스튜디오
- 개발
- Android Studio
- 장고
- 중국어입문
- PYTHON
- MernStack
- 안드로이드스튜디오
- mern Stack
- 중국어
- androidstudio
- 알고리즘
- 파이썬
- nodejs
- 블로그 만들기
- 리액트
- 자바
- Django
- express
- 블로그만들기
- Android
- react
Archives
- Today
- Total
City At Night
[Android Studio] Navigation Drawer 사이드 메뉴바 커스텀 본문
728x90
반응형
화면을 왼쪽에서 오른쪽으로 쓸어넘기거나 메뉴 버튼을 누르면 원하는 위치에 메뉴바가 나오게 한다.
MainActivity에서 네비게이션 드로어 동작을 호출하며
네비게이션 드로어.XML 을 따로 만든다.
<MainActivity.java>
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private View drawerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerView = (View)findViewById(R.id.drawer2);
//drawerLayout.setDrawerListener(listener);
drawerLayout.addDrawerListener(listener);
drawerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
DrawerLayout.DrawerListener listener = new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
//슬라이드 했을때
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
//Drawer가 오픈된 상황일때 호출
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
// 닫힌 상황일 때 호출
}
@Override
public void onDrawerStateChanged(int newState) {
// 특정상태가 변결될 때 호출
}
};
public void btnOnclick(View view) {
switch (view.getId()){
case R.id.btn_open:
drawerLayout.openDrawer(drawerView);
}
}
}
setDrawerListener(listener)은 deprecated 이기때문에
addDrawerListener(listener)로 바꿔준다.
<activity_main.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnOnclick"
android:text="버튼이에요" />
</LinearLayout>
<include layout="@layout/navbar" />
</androidx.drawerlayout.widget.DrawerLayout>
<navbar.xml>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="198dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/drawer2">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="198dp"
android:layout_height="173dp"
android:background="#33F5D028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="70dp"
android:text="개발자님"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_close1" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginBottom="22dp"
android:text="안녕하세요"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn_close1"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_marginStart="18dp"
android:layout_marginTop="15dp"
android:background="@drawable/ic_vectorxmark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="43dp"
android:text="홈"
android:textColor="#323333"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="23dp"
android:text="소개"
android:textColor="#323333"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="23dp"
android:text="사용법"
android:textColor="#323333"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="23dp"
android:text="그룹"
android:textColor="#323333"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="23dp"
android:text="마이페이지"
android:textColor="#323333"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="136dp"
android:text="문의"
android:textColor="#000000"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:text="062 123 4567"
android:textColor="#000000"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
</androidx.constraintlayout.widget.ConstraintLayout>
728x90
반응형
'Android Studio' 카테고리의 다른 글
[Android Studio] Retrofit2를 사용하여 Api파싱 (0) | 2021.01.31 |
---|---|
[Android Studio] Splash! 앱의 Intro를 만들어보자 (0) | 2021.01.31 |
[Android Studio]WebView를 사용하여 Web을 App처럼 사용하기 (2) | 2021.01.31 |
[Android Studio] RecyclerView를 사용하여 데이터를 추가해보자! (0) | 2021.01.30 |
[Android Studio] SharedPreferences로 간단한 데이터 저장하기! (0) | 2021.01.24 |
Comments