Android抽屉页面效果
2019獨角獸企業重金招聘Python工程師標準>>>
DrawerLayout抽屜布局,現在主流App是越來越多使用DrawerLayout,因為這樣出來的效果是比較炫酷的吧!其實抽屜界面很簡單,沒有網上說的那么復雜,今天我就給大家介紹一種比較簡單的抽屜布局,DrawerLayout,
效果圖:
1:主布局文件(分為2塊,第一塊是主頁面內容,第二塊是抽屜頁面內容,這是固定的,必須是這樣的格式)
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
??? xmlns:tools= "http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical"
??? android:paddingBottom="@dimen/activity_vertical_margin"
??? android:paddingLeft="@dimen/activity_horizontal_margin"
??? android:paddingRight="@dimen/activity_horizontal_margin"
??? android:paddingTop="@dimen/activity_vertical_margin"
??? tools:context=".MainActivity" >
??? <android.support.v4.widget.DrawerLayout
??????? android :id="@+id/drawer_layout"
??????? android :layout_width="match_parent"
??????? android :layout_height="match_parent">
???????
??????? <!-- 主界面-->
???????
??????? <LinearLayout
??????????? android:id="@+id/content_frame"
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:background="#FF0000">
??????????? <Button
??????????????? android:id= "@+id/btn"
??????????????? android:layout_width= "match_parent"
??????????????? android:layout_height= "wrap_content"
??????????????? android:text= "open" />
??????? </LinearLayout >
??????? <!-- 抽屜界面 --> //抽屜頁面可以是一個Fragment,或者是ListView,什么都可以,這里以Fragment為列子,因為Fragment可以添加一個布局文件
??????? <FrameLayout
??????????? android:id="@+id/fragment_layout"
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_gravity="start"//從左邊滑動進來,右邊是end,一定要寫,不然沒有效果
??????????? android:background="#9053ff59"></ FrameLayout>
??? </android.support.v4.widget.DrawerLayout >
</LinearLayout >
2:Fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical" >
??? <Button
??????? android :layout_width="match_parent"
??????? android :layout_height="wrap_content"
??????? android :text="按鈕一"/>
??? <Button
??????? android :layout_width="match_parent"
??????? android :layout_height="wrap_content"
??????? android :text="按鈕二"/>
??? <Button
??????? android :id="@+id/item_frgment_bt"
??????? android :layout_width="match_parent"
??????? android :layout_height="wrap_content"
??????? android :text="按鈕三"/>
??? <Button
??????? android :layout_width="match_parent"
??????? android :layout_height="wrap_content"
??????? android :text="按鈕四"/>
</LinearLayout >
4:Fragment類
package com.example.yangjie.drawerlayout;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by Administrator on 2015/10/17.
*/
public class DrawerFragment extends Fragment {
??? @Nullable
??? @Override
??? public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
??????? return inflater.inflate(R.layout.item_frgement,container,false);//將布局文件綁定到Fragment中
??? }
??? @Override
??? public void onViewCreated(View view, Bundle savedInstanceState) {
??????? super .onViewCreated(view, savedInstanceState);
??????? Button? button=(Button)view.findViewById(R.id.item_frgment_bt);
??????? button.setOnClickListener(new View.OnClickListener() { //給抽屜界面的按鈕散設置了一個箭頭
??????????? @Override
??????????? public void onClick(View v) {
??????????????? Toast.makeText (getActivity(),"點擊了按鈕3" ,Toast.LENGTH_SHORT).show();
??????????? }
??????? });
??? }
}
5:Activity
public class MainActivity extends AppCompatActivity {
??? private DrawerLayout mDrawerLayout = null ;
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super .onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? FragmentManager fragmentManager=getFragmentManager();
??????? FragmentTransaction ft=fragmentManager.beginTransaction();
??????? DrawerFragment? drawerFragment=new DrawerFragment();
??????? ft.add(R.id.fragment_layout,drawerFragment,"標簽1");//將fragment綁定到已經占有位置的FrameLayout中
??????? ft.commit();
??????? mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
??????? Button button = (Button) findViewById(R.id.btn);
??????? button.setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? // 按鈕按下,將抽屜打開
??????????????? mDrawerLayout.openDrawer(Gravity. LEFT); //從左打開
??????????? }
??????? });
??? }
}
轉載于:https://my.oschina.net/u/2502508/blog/537409
總結
以上是生活随笔為你收集整理的Android抽屉页面效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 菜鸟关于mvc导出Excel的想法
- 下一篇: MySQL 5.6x开启慢查询日志