DrawerLayout 侧滑栏

一、DrawerLayout侧滑栏

常见的客户端中左右侧滑栏一般都使用此控件,首先在项目Modle.gradle 中添加依赖包

implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:palette-v7:28.0.0'

简单示例如下:
(1)界面布局


    
    
        
        
        
            

(2)MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private TextView ll_content;
    private LinearLayout id_ll_left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.id_dl_root);
        ll_content = findViewById(R.id.ll_text);
        id_ll_left = findViewById(R.id.id_ll_left);

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {   //监听drawerLayout
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {
                id_ll_left.setRotation(360 * v);    //360°旋转
                ll_content.setScaleX(2 * v);     //放大2倍
                ll_content.setScaleY(2 * v);
            }

            @Override
            public void onDrawerOpened(@NonNull View view) {

            }

            @Override
            public void onDrawerClosed(@NonNull View view) {

            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });

        findViewById(R.id.lf_but).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.closeDrawer(Gravity.START);
                ll_content.setText("左侧栏被点击了");
            }
        });

        findViewById(R.id.open_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });

        findViewById(R.id.open_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.END);
            }
        });
    }
}

DrawerLayout 侧滑栏_第1张图片

你可能感兴趣的:(Android,DrawerLayout)