Android简单的自定义抽屉布局(DrawerLayout)

采用DrawerLayout的方法实现抽屉布局的效果,抽屉布局有两个部分,第一个部分为主内容区,如图中的包含底部菜单栏的部分。第二部分为侧边栏部分,左边部分。

对应xml布局如下:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#eeeeee"
        >
        <include
            android:id="@+id/include2"
            layout="@layout/top_search_view"
            android:layout_width="match_parent"
            android:layout_height="100px"
            android:layout_alignParentTop="true"
            ></include>

        <FrameLayout
            android:id="@+id/fl_drawerlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/include2"
            android:layout_above="@+id/textview"
            />
        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#bfbfbf"
            android:layout_above="@+id/include"
            android:layout_alignParentStart="true" />
        <include
            android:id="@+id/include"
            layout="@layout/bottom_menu"
            android:layout_width="match_parent"
            android:layout_height="100px"
            android:layout_alignParentBottom="true"
            ></include>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/ll_left_menus"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_width="250dp"
        android:orientation="vertical">
        <include
            layout="@layout/left_menu"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            >
        </include>
    </LinearLayout>




</android.support.v4.widget.DrawerLayout>

其中的RelativeLayout布局所包含的内容即为主内容,其中第一个include进的布局为标题栏,类似于actionbar,第二个include进的为底部菜单栏,其中的FrameLayout用来填充fragment,点击对应的底部菜单后添加对应的fragment。

LinearLayout布局则对应的是侧边栏,include进来的是一个自定义布局,包括头像,名字,功能列表等。其中layout_gravity属性的值设为start则表示侧边栏从左往右滑动开启,设置为end则表示从右往左滑动开启。



类文件如下:

package com.xiaoyi.Main;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaoyi.Company.CompanyFragment;
import com.xiaoyi.Friend.FriendFragment;
import com.xiaoyi.Message.MessageFragment;
import com.xiaoyi.position.PositionFragment;


public class MainActivity extends Activity{

    private ImageView iv_head,iv_left_setting; //用户头像和设置按钮
    private ImageView iv_bottom_menu0,iv_bottom_menu1,iv_bottom_menu2,iv_bottom_menu3;//底部菜单
    private ImageView iv_left_menu0,iv_left_menu1,iv_left_menu2,iv_left_menu3; //抽屉中的菜单
    private TextView tv_top_name; //顶部标题栏文字
    private ImageView iv_search; //搜索按钮
    private ImageView iv_drawer; //开启抽屉的点击图标
    private DrawerLayout drawer_main; //抽屉
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }
    public void initView(){
        iv_head = (ImageView)findViewById(R.id.iv_head);
        iv_left_setting = (ImageView)findViewById(R.id.iv_left_setting);

        iv_bottom_menu0 = (ImageView)findViewById(R.id.iv_bottom_menu0);
        iv_bottom_menu1 = (ImageView)findViewById(R.id.iv_bottom_menu1);
        iv_bottom_menu2 = (ImageView)findViewById(R.id.iv_bottom_menu2);
        iv_bottom_menu3 = (ImageView)findViewById(R.id.iv_bottom_menu3);

        iv_left_menu0 = (ImageView)findViewById(R.id.iv_left_menu0);
        iv_left_menu1 = (ImageView)findViewById(R.id.iv_left_menu1);
        iv_left_menu2 = (ImageView)findViewById(R.id.iv_left_menu2);
        iv_left_menu3 = (ImageView)findViewById(R.id.iv_left_menu3);

        tv_top_name = (TextView)findViewById(R.id.tv_top_name);
        iv_search = (ImageView)findViewById(R.id.iv_search);
 

        BottomMenuOnclick();//底部菜单的点击事件监听
        LeftMenuOnclick(); //侧边栏菜单的点击事件的监听

        drawer_main = (DrawerLayout) findViewById(R.id.drawer_main);

        iv_drawer = (ImageView)findViewById(R.id.iv_drawer);
        iv_drawer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer_main.openDrawer(GravityCompat.START); //点击按钮后打开抽屉,START为从左向右打开,END为从右向左打开,跟xml布局中的属性一直
            }
        });

    }

    public void LeftMenuOnclick(){
        iv_left_menu0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        iv_left_menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        iv_left_menu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        iv_left_menu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    public void BottomMenuOnclick(){

        //初始时为职位fragment页面
        iv_bottom_menu0.setBackgroundResource(R.drawable.position_select);
        Fragment positionFragment = new PositionFragment();
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.fl_drawerlayout,positionFragment).commit();
        tv_top_name.setText("职位");

        iv_bottom_menu0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_bottom_menu0.setBackgroundResource(R.drawable.position_select);
                iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
                iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
                iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);

                Fragment positionFragment = new PositionFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.fl_drawerlayout, positionFragment).commit();
                tv_top_name.setText("职位");
                iv_search.setVisibility(View.VISIBLE);
            }
        });
        iv_bottom_menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
                iv_bottom_menu1.setBackgroundResource(R.drawable.company_select);
                iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
                iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);

                Fragment companyFragment = new CompanyFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.fl_drawerlayout, companyFragment).commit();
                tv_top_name.setText("企业");
                iv_search.setVisibility(View.VISIBLE);
            }
        });

        iv_bottom_menu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
                iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
                iv_bottom_menu2.setBackgroundResource(R.drawable.message_select);
                iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);

                Fragment messageFragment = new MessageFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.fl_drawerlayout, messageFragment).commit();
                tv_top_name.setText("消息");
                iv_search.setVisibility(View.INVISIBLE);
            }
        });

        iv_bottom_menu3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
                iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
                iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
                iv_bottom_menu3.setBackgroundResource(R.drawable.friend_select);

                Fragment friendFragment = new FriendFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.fl_drawerlayout, friendFragment).commit();
                tv_top_name.setText("朋友");
                iv_search.setVisibility(View.INVISIBLE);
            }
        });
    }
}


由于不知道什么原因,如果你用activity直接继承OnClickListener()的方式监听点击事件的话并没有效果,因此本人采用直接设置监听器的方式来监听点击事件。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抽屉布局Android开发中常用的一种布局方式,它可以实现侧滑菜单的效果。在Android Studio中,可以通过以下步骤来使用抽屉布局: 首先,在你的Android项目中打开activity_main.xml文件,在布局文件中添加一个DrawerLayout作为根布局DrawerLayout是一个特殊的布局容器,它允许一个子视图作为主视图,一个子视图作为抽屉视图。你可以使用以下代码将DrawerLayout添加到布局文件中: ``` <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 主视图 --> <RelativeLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 主视图的内容 --> </RelativeLayout> <!-- 抽屉视图 --> <LinearLayout android:id="@+id/drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:orientation="vertical"> <!-- 抽屉视图的内容 --> </LinearLayout> </android.support.v4.widget.DrawerLayout> ``` 然后,在MainActivity.java文件中,你可以通过以下代码来处理抽屉布局的逻辑: ``` public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // 初始化ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close); drawerLayout.addDrawerListener(drawerToggle); // 设置ActionBarDrawerToggle为ActionBar的导航按钮 getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // 同步ActionBarDrawerToggle的状态 drawerToggle.syncState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { // 处理ActionBarDrawerToggle的点击事件 if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } ``` 在上述代码中,我们使用了ActionBarDrawerToggle来实现ActionBar和抽屉布局的联动。可以通过点击ActionBar上的导航按钮来打开和关闭抽屉菜单。 需要注意的是,在使用抽屉布局时,你需要为主视图和抽屉视图分别添加内容。你可以根据自己的需求来自定义主视图和抽屉视图的内容,并在代码中处理相应的逻辑。 希望这些信息对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值