简单DrawerLayout与ActionBar的结合使用

1. 布局

<android.support.v4.widget.DrawerLayout
    android:id="@+id/i_drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    //主界面
    <LinearLayout
        android:layout_width="match_parent"
        android:background="#f0f"
        android:layout_height="match_parent">

    </LinearLayout>
    //侧滑菜单
    <LinearLayout
        android:background="#00f"
        android:gravity="start"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

2. 改为该界面继承Activity 

MainActivity extends Activity

3. 改变主题

<style name="AppTheme" parent="android:Theme.Material">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

4. 写主要逻辑代码

public class MainActivity extends Activity {
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle drawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout= (DrawerLayout) findViewById(R.id.i_drawerLayout);
        drawerToggle=new ActionBarDrawerToggle(this,drawerLayout,0,0);
        drawerLayout.setDrawerListener(drawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        drawerToggle.onOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        drawerToggle.onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值