亲宝软件园·资讯

展开

Android布局控件DrawerLayout实现侧滑效果 Android布局控件DrawerLayout实现完美侧滑效果

adolph_jun 人气:0

drawerLayout其实是一个布局控件,跟LinearLayout等控件是一样的,但是drawerLayout带有滑动的功能。只要按照drawerLayout的规定布局方式写完布局,就能有侧滑的效果。

1)在DrawerLayout中,第一个子View必须是显示内容的view,并且设置它的layout_width和layout_height属性是match_parent.

2)第二个view是抽屉view,并且设置属性layout_gravity="left|right",表示是从左边滑出还是右边滑出。设置它的layout_height="match_parent"

ActionBarDrawerToggle就是DrawerLayout事件的监听器。
ActionBarDrawerToggle有3个方法可以被复写,分别用来实现DrawerLayout打开,关闭,滑动的事件监听:

drawerLayout布局代码:

<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"
  >
  //主内容
  <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>
  //侧滑菜单
  <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代码

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代码

<resources>
  <string name="app_name">DrawerLayoutlearen</string>
  <string name="open" />
  <string name="close" />
</resources>

加载全部内容

相关教程
猜你喜欢
用户评论