Android layout control DrawerLayout realizes perfect sideslip effect
- 2021-10-13 08:34:40
- OfStack
drawerLayout is actually a layout control, like LinearLayout and other controls, but drawerLayout has a sliding function. As long as the layout is written according to the layout mode specified in drawerLayout, it can have sideslip effect.
1) In DrawerLayout, the first child View must be the view that displays the content and set its layout_width and layout_height properties to match_parent.
2) The second view is the drawer view, and the property layout_gravity= "leftright" is set to indicate whether to slide out from the left or the right. Set its layout_height= "match_parent"
ActionBarDrawerToggle is the listener for DrawerLayout events.
ActionBarDrawerToggle has three methods that can be replicated, which are used to monitor DrawerLayout opening, closing and sliding events:
drawerLayout layout code:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
>
// Main content
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/drawer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"/>
</FrameLayout>
// Sideslip menu
<ListView
android:id="@+id/left_drawer"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
mainactivity. java code
package com.example.wxj.drawerlayoutlearen;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private TextView view;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
view = (TextView)findViewById(R.id.drawer_text);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.open,
R.string.close
){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
view.setText("close");
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
view.setText("dakai");
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
view.setText("huachu");
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
string. xml code
<resources>
<string name="app_name">DrawerLayoutlearen</string>
<string name="open" />
<string name="close" />
</resources>