Xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
1
2
3
4
5
6<FrameLayout android:id="@+id/fragment" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
Activity:
1
2package com.example.work9_10;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
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.MenuItem;
public class MainActivity extends AppCompatActivity {
//定义变量
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取资源ID drawer=findViewById(R.id.drawer); //添加侧滑布局 if (savedInstanceState==null){ getSupportFragmentManager().beginTransaction() //侧滑的id;要为侧滑添加的布局 .add(R.id.fragment,new leftFragment()) .commit(); } initData(); } private void initData() { //允许标题栏展示左边图标 getSupportActionBar().setDisplayHomeAsUpEnabled(true); //图标toogle和侧滑联系 toggle=new ActionBarDrawerToggle(this,drawer,R.string.open_drawer,R.string.close_drawer); drawer.addDrawerListener(toggle); toggle.syncState(); } //点击图标,打开或关闭侧滑 public boolean onOptionsItemSelected(MenuItem item){ if (toggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }
}
为侧滑添加的布局(只对布局加了背景):
package com.example.work9_10;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class leftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.leftfragment,container,false);
return view;
}
}
最后
以上就是漂亮高跟鞋最近收集整理的关于左边侧滑与标题栏图标交互的全部内容,更多相关左边侧滑与标题栏图标交互内容请搜索靠谱客的其他文章。
发表评论 取消回复