国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

DrawerLayout實現側滑仿QQ界面

2019-11-09 15:48:36
字體:
來源:轉載
供稿:網友

簡介

可以說drawerLayout是因為第三方控件如MenuDrawer等的出現之后,google借鑒而出現的產物。 drawerLayout分為側邊菜單和主內容區兩部分,側邊菜單可以根據手勢展開與隱藏(drawerLayout自身特性), 主內容區的內容可以隨著菜單的點擊而變化(這需要使用者自己實現)。


1.編寫Activity的布局文件 根布局使用android.support.v4.widget.DrawerLayout 然后其內部第一個View為內容區域,第二個View為左側菜單,第三個View為右側側滑菜單,當前第三個是可選的。 第一個View的寬高應當設置為match_parent,當然了,這也理所當然。 第二、三個View需要設置Android:layout_gravity=”left”,和android:layout_gravity=”right”且一搬高度設置為match_parent, 寬度為固定值,即側滑菜單的寬度(寬度你寫match_parent也行,但是不會全部覆蓋)。 首先是mainactivity_layout.xml

<?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/mdrawerLayout"android:layout_width="match_parent" android:layout_height="match_parent"tools:context="com.soft.qianyu.myQQdddreawerlayout.MainActivity"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#60f227" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="35sp" android:text="我是內容界面"/> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打開側滑菜單" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#4b91f9" android:id="@+id/left" android:layout_width="match_parent" android:layout_gravity="left" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="35sp" android:text="我是側滑菜單"/> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> </LinearLayout></android.support.v4.widget.DrawerLayout>

寫完布局之后,你就可以實現側滑了!但是此時的側滑并不是將內容界面擠到右側的側滑,而是覆蓋在內容界面上面的側滑。此時若還要實現像QQ那樣的側滑,則還需要以下代碼。

2,在代碼中為相應的控件添加事件監聽器

public class MainActivity extends Activity implements View.OnClickListener{ //1)聲明DrawerLayout變量 DrawerLayout drawerLayout; //聲明內容頁面變量 LinearLayout contentView ; //聲明按鈕 Button bt1,bt2; @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 init(); }private void init() {//2)實例化DrawerLayout控件drawerLayout = (DrawerLayout) findViewById(R.id.mdrawerLayout);//實例化按鈕并設置點擊監聽事件findViewById(R.id.bt1).setOnClickListener(MainActivity.this);findViewById(R.id.bt2).setOnClickListener(MainActivity.this);//3)為DrawerLayout控件添加監聽器drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() { //當側滑菜單正在滑動時觸發的方法 /** 第一個參數:正在滑動的側滑菜單 第二個參數:菜單滑動的寬度的百分比 **/ @Overridepublic void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset);//獲得側滑菜單的寬度int drawerViewWidth = drawerView.getMeasuredWidth();//根據滑動百分比計算內容部分應該向右邊移動的距離int marginLeft = (int)(drawerViewWidth * slideOffset) ;//獲得內容部分的View對象(內容View對象是第一個,所以是0)contentView = (LinearLayout) drawerLayout.getChildAt(0);//修改內容部分的左邊距contentView.setLeft(marginLeft); } });} @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt1: drawerLayout.openDrawer(Gravity.LEFT); break; case R.id.bt2: drawerLayout.closeDrawers(); break; } }}
上一篇:Glide加載圖片

下一篇:ffmpeg學習

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 门头沟区| 永胜县| 岳普湖县| 株洲市| 丰宁| 五大连池市| 汉阴县| 湖南省| 宁阳县| 沭阳县| 泰州市| 上虞市| 鹤壁市| 咸宁市| 当雄县| 巴里| 微博| 霞浦县| 大渡口区| 溧水县| 宣汉县| 依安县| 邵阳县| 郑州市| 长春市| 连平县| 壶关县| 盐城市| 富蕴县| 绵竹市| 政和县| 根河市| 云阳县| 嘉鱼县| 文安县| 兖州市| 区。| 镇安县| 弋阳县| 福泉市| 富顺县|