Android-DrawerLayout的进一步学习

先看下效果



实现侧滑和点击Imageview实现切换图片

看了别人的一个UI框架,但是目前还有很多东西不会,就先弄个简单的玩玩

接下来直接上代码了

首先是布局文件,注意一个先后顺序,内容在前,策划菜单在后

<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wxj_try.myui.MainActivity">
    <!--先进行占位-->
    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/bg"/>

    </FrameLayout>
    <ScrollView
        android:id="@+id/scroll_View"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#33334c">
        <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_close"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_close"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_1"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_1"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_2"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_2"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id ="@+id/iv_3"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_3"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_4"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_4"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_5"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_5"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_6"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_6"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#000000"/>
            <ImageView
                android:id="@+id/iv_7"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:src="@drawable/icn_7"/>
        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.DrawerLayout>


接下来是java代码;

package com.example.wxj_try.myui;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import static com.example.wxj_try.myui.R.id.iv;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout mFrameLayout;
    private LinearLayout mLinearLayout;
    private ScrollView mScrollView;
    private DrawerLayout mDrawerLayout;
    private Toolbar toolbar;
    private ImageView imageView;
    private ImageView Iv_close, Iv_1, Iv_2, Iv_3, Iv_4, Iv_5, Iv_6, Iv_7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        // 设置ActionBar可见,并且切换菜单和内容视图
       /* getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);*/
    }

    private void init() {
        mFrameLayout = (FrameLayout) findViewById(R.id.frameLayout);
        mLinearLayout = (LinearLayout) findViewById(R.id.left_drawer);
        mScrollView = (ScrollView) findViewById(R.id.scroll_View);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        imageView = (ImageView) findViewById(R.id.iv);
        Iv_1 = (ImageView) findViewById(R.id.iv_1);
        Iv_2 = (ImageView) findViewById(R.id.iv_2);
        Iv_3 = (ImageView) findViewById(R.id.iv_3);
        Iv_4 = (ImageView) findViewById(R.id.iv_4);
        Iv_5 = (ImageView) findViewById(R.id.iv_5);
        Iv_6 = (ImageView) findViewById(R.id.iv_6);
        Iv_7 = (ImageView) findViewById(R.id.iv_7);
        Iv_close = (ImageView) findViewById(R.id.iv_close);

        Iv_1.setOnClickListener(this);
        Iv_2.setOnClickListener(this);
        Iv_3.setOnClickListener(this);
        Iv_4.setOnClickListener(this);
        Iv_5.setOnClickListener(this);
        Iv_6.setOnClickListener(this);
        Iv_7.setOnClickListener(this);
        Iv_close.setOnClickListener(this);

        mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现点击侧边栏按钮后,侧边栏自动收回

            }
        });
        CreateMenuList();
    }

    private void CreateMenuList() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_1:
                Toast.makeText(MainActivity.this, "" + v.getId(), Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.my1);
                break;
            case R.id.iv_2:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.dushu);
                break;
            case R.id.iv_3:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.huahua);
                break;
            case R.id.iv_4:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.gongzuo);
                break;
            case R.id.iv_5:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.gouwu);
                break;
            case R.id.iv_6:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.content_music);
                break;
            case R.id.iv_7:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.content_films);
                break;
            case R.id.iv_close:
                Toast.makeText(MainActivity.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                imageView.setImageResource(R.drawable.bg);
                break;
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值