Android之 DrawerLayout不覆蓋主內容
阿新 • • 發佈:2019-01-09
DrawerLayout不覆蓋主內容 並實現側滑的點選
Activity
/*
drawerlayout不覆蓋主內容
並實現側滑中的點選事件
*/
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private TextView tv1, tv2, tv3, tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
tv1.setOnClickListener(new MyClickListener());
tv2.setOnClickListener(new MyClickListener());
tv3.setOnClickListener(new MyClickListener());
tv4.setOnClickListener(new MyClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
mDrawerLayout.setScrimColor(Color.TRANSPARENT);//側滑選單開啟後主內容區域的顏色
mDrawerLayout.addDrawerListener(drawerListener);
}
class MyClickListener implements DrawerLayout.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv1:
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this, "-->" + view.getId(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv2:
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this, "-->" + view.getId(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv3:
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this, "-->" + view.getId(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv4:
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this, "-->" + view.getId(), Toast.LENGTH_SHORT).show();
break;
}
}
}
DrawerLayout.DrawerListener drawerListener = new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// 得到contentView
View content = mDrawerLayout.getChildAt(0);
int offset = (int) (drawerView.getWidth() * slideOffset);
content.setTranslationX(offset);
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
};
}
activity_main.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/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="driving.hezejiaxiao.com.newdrawerlayout.MainActivity">
<TextView
android:id="@+id/tvcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="內容區"
android:textSize="50sp" />
<LinearLayout
android:id="@+id/leftlayout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_green_dark"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="側滑"
android:textSize="40sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="側滑"
android:textSize="40sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="側滑"
android:textSize="40sp" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="側滑"
android:textSize="40sp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
貼上以上程式碼就可以執行啦!