Android Studio to achieve sliding bar

1. sliding column layout , (the name can define your own, do not care.) To delete all changed

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="start"

android:clickable="true">
</FrameLayout>

 

2. Create another .xml, named on behalf of our main interface activity_main. For the main layout

3. (main thruster layout and layout together)

Then create a .xml, named custom_drawerlayout represent our final product.

 

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/activity_main"/>"
</LinearLayout>
<!--侧滑菜单-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_gravity="start">
<include layout="@layout/left_menu"/>
</ LinearLayout>
</android.support.v4.widget.DrawerLayout>
to pay attention to a .xml file that gives a id, because we certainly want to back them operate in the Java code.

 

3. Then create MainActivity.jar in the java folder

Examples of the sliding button bar for displaying a DrawerLayout and improve code below.
 

public class MainActivity extends Activity{

        private DrawerLayout mDrawerLayout;

        private Button btn;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//汇总布局
setContentView(R.layout.custom_drawerlayout);

        mDrawerLayout = findViewById(R.id.dl_left);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDrawerLayout();
            }
        });

}


//侧推栏效果
private void showDrawerLayout() {

//可更改方向
if (!mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.openDrawer(Gravity.LEFT);
} else {
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
}

}

Guess you like

Origin www.cnblogs.com/sunjian43792901/p/11130431.html