概述
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">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
Activity:
package 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;
@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;
}
}
最后
以上就是漂亮高跟鞋为你收集整理的左边侧滑与标题栏图标交互的全部内容,希望文章能够帮你解决左边侧滑与标题栏图标交互所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复