侧滑界面的实现
侧滑界面——向右滑动或者是点击左上角的一个文本进行侧滑
主界面activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:padding="10dp" 8 android:id="@+id/drawerLayout" 9 tools:context=".MainActivity"> 10 11 <RelativeLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:layout_marginLeft="10dp"> 15 16 <TextView 17 android:id="@+id/slide_name" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="侧滑" 21 android:layout_marginTop="10dp" 22 android:textSize="15sp" 23 android:textColor="#000000" 24 /> 25 26 </RelativeLayout> 27 28 <!--侧滑界面,必须设置layout_gravity属性,表示侧滑方向--> 29 <RelativeLayout 30 android:layout_gravity="start" 31 android:id="@+id/ll" 32 android:background="#FFFF00" 33 android:orientation="vertical" 34 android:layout_width="300dp" 35 android:layout_height="match_parent"> 36 37 <Button 38 android:id="@+id/btn_add" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" 41 android:text="添加账户" 42 android:layout_marginTop="20dp" 43 android:background="#1E86FD" 44 /> 45 46 </RelativeLayout> 47 48 </androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
1 public class MainActivity extends AppCompatActivity{ 2 //侧滑 3 private TextView mSlideName; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 //侧滑 9 final DrawerLayout drawerLayout=findViewById(R.id.drawerLayout); 10 //给按钮添加一个监听器 11 mSlideName=findViewById(R.id.slide_name); 12 Intent intent = getIntent(); 13 mSlideName.setText(intent.getStringExtra("user_name")); 14 mSlideName.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View view) { 17 //打开侧滑菜单 18 drawerLayout.openDrawer(GravityCompat.START); 19 } 20 }); 21 } 22 }
以上便是完成侧滑的相应步骤