侧边栏的使用是非常的常见,一大部分的App都会使用,可以当做显示用户的信息,作为跳转控件等都可以在侧边栏显示,而这个侧边栏的使用是直接用系统内部来实现了,非常方便。
1.xml文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ce" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#a2e668" android:id="@+id/ce_iv" /> <Button android:id="@+id/ce_but" android:background="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击打开侧边栏" /> </RelativeLayout> <android.support.design.widget.NavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" android:id="@+id/ce_ntv" > </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
注意:这里必须是以DraverLayout为根控件,NacigationView是侧边栏控件,通过DrawerLayout来设置侧边栏的位置
2.侧边栏布局文件(ce_layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:background="#f28d8d" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/ce_tv1" android:text="按钮1" android:textSize="50dp" android:clickable="true" > </TextView> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/ce_tv2" android:text="按钮2" android:textSize="50dp" android:clickable="true" > </TextView> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/ce_tv3" android:text="按钮3" android:textSize="50dp" android:clickable="true" > </TextView> </LinearLayout>
3.Java文件
public class CeActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private NavigationView navigationView; private DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ce); getSupportActionBar().hide(); button=(Button)findViewById(R.id.ce_but); navigationView=(NavigationView)findViewById(R.id.ce_ntv); drawerLayout=(DrawerLayout)findViewById(R.id.activity_ce); //设置侧边栏 setCeData(); //控件点击可以打开侧边栏 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //打开侧边栏 drawerLayout.openDrawer(GravityCompat.START); } }); } private void setCeData(){ //侧边栏绑定布局 navigationView.inflateHeaderView(R.layout.ce_layout); //通过侧边栏获取View View view =navigationView.getHeaderView(0); //通过View来获取view上面的控件,然后就可以做逻辑了 TextView textView1=(TextView) view.findViewById(R.id.ce_tv1); TextView textView2=(TextView) view.findViewById(R.id.ce_tv2); TextView textView3=(TextView) view.findViewById(R.id.ce_tv3); textView1.setOnClickListener(this); textView2.setOnClickListener(this); textView3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.ce_tv1: Toast.makeText(CeActivity.this,"1111",Toast.LENGTH_SHORT).show(); //关闭侧边栏 drawerLayout.closeDrawers(); break; case R.id.ce_tv2: Toast.makeText(CeActivity.this,"2222",Toast.LENGTH_SHORT).show(); drawerLayout.closeDrawers(); break; case R.id.ce_tv3: Toast.makeText(CeActivity.this,"3333",Toast.LENGTH_SHORT).show(); break; } } }
这样就非常永远的实现的侧边栏的功能。