Android 界面侧滑
DrawableLayout 抽屉布局
在activity布局文件中加入DrawableLayout
控件
<android.support.v4.widget.DrawerLayout
android:id="@+id/main_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="第一个TextView里面的内容"
android:background="@color/colorPrimary"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="start|left"
android:text="第二个TextView里面的内容"
android:background="@color/colorAccent"/>
</android.support.v4.widget.DrawerLayout>
- 这里第一个
TextView
为显示界面,第二个TextView
是抽屉可拉伸; - 第二个
TextView
设置的layout_gravity
表明了抽屉的拉出位置
抽屉侧滑结合ActionBar 的左侧按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawer = (DrawerLayout) findViewById(R.id.main_drawer);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("ActionBar");
actionBar.setDisplayHomeAsUpEnabled(true);
mToggle = new ActionBarDrawerToggle(this,mDrawer,0,0);
mDrawer.addDrawerListener(mToggle);
mToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
mToggle.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}
- 这里使用activity默认的
ActionBar
- 获取
ActionBar
设置标题 --> 设置左侧按钮可用 - 创建
ActionBarDrawerToggle
对象,抽屉的触发器,开关 - 抽屉关联开关
mDrawer.addDrawerListener(mToggle);
--> 抽屉的动作会影响开关 - 开关同步抽屉状态
mToggle.syncState();
--> 点击开关会影响抽屉
抽屉侧滑结合Toolbar
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.main_drawer);
setSupportActionBar(mToolbar);
ActionBar actionBar = getSupportActionBar();
mToggle = new ActionBarDrawerToggle(this,mDrawer,mToolbar,0,0);
mDrawer.addDrawerListener(mToggle);
mToggle.syncState();
}
- 需要改写activity的
Theme
设置为NoActionBar
SlidingPaneLayout
GroupView的子类
android.support.v4.widget.SlidingPaneLayout
创建BaseActivity让所有的Activity都具有侧滑放回功能。
创建BaseActivity
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/base_sliding"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.doublefang.slidingpanelayoutdemo.BaseActivity">
<View
android:id="@+id/base_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<FrameLayout
android:id="@+id/base_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
- 第一个
View
为滑动的后页面 - 第二个
LinearLayout
是正常展示界面 - 这里的
FrameLayout
为Activity
站位 android:layout_weight="1"
BaseActivity代码:
public class BaseActivity extends AppCompatActivity implements SlidingPaneLayout.PanelSlideListener {
private Toolbar mToolbar;
private SlidingPaneLayout mSliding;
private FrameLayout mContent;
private View mPanel;
// private boolean slidable = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_base);
// setTheme(R.style.MyTheme);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mSliding = (SlidingPaneLayout) findViewById(R.id.base_sliding);
mSliding.setPanelSlideListener(this);
mSliding.setSliderFadeColor(0);
mContent = (FrameLayout) findViewById(R.id.base_content);
mPanel = findViewById(R.id.base_view);
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
View view = LayoutInflater.from(this).inflate(layoutResID, mContent, false);
setContentView(view);
}
@Override
public void setContentView(View view) {
view.setBackgroundColor(Color.WHITE);
mContent.addView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
view.setBackgroundColor(Color.WHITE);
mContent.addView(view);
}
@Override
public void onPanelSlide(View panel, float slideOffset) { }
@Override
public void onPanelOpened(View panel) {
finish();
}
@Override
public void onPanelClosed(View panel) { }
public void setSlidable(boolean flag) {
// this.slidable = flag;
if (!flag) mSliding.removeView(mPanel);
}
}
- 滑动切换界面的时候默认是上面的界面会变透明,设置取消透明
mSliding.setSliderFadeColor(0);
-
view.setBackgroundColor(Color.WHITE);
防止正常显示的界面透明
定制AppTheme
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@style/WindowAnimation</item>
</style>
<style name="WindowAnimation">
<item name="android:activityOpenEnterAnimation">@android:anim/slide_in_left</item>
<item name="android:activityCloseExitAnimation">@android:anim/slide_out_right</item>
</style>
NoActionBar
- 设置窗口为透明,如果
View
控件没有设置背景那就是透明的 - 设置动画:默认活动是从底部出现。