關於如何生成側邊欄這件事


更改activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.vincent.showetf.MainActivity"
    android:id="@+id/drawerLayout">
    <!-- center 主程式位置-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:textSize="40sp" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        app:menu="@menu/activity_menu" />

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

生成menu的資料夾
如下圖

createDirectory
接著生成menu的resource.file
activity_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/showProduct"
            android:title="@string/showProduct"/>
        <item
            android:id="@+id/feedback"
            android:title="@string/feedback"/>
    </group>
</menu>


更改values資料夾裡的string檔
<resources>
    <string name="app_name">showProduct</string>
    <string name="showProduct">展示商品</string>
    <string name="feedback">意見反饋</string>
</resources>

JAVA檔


import android.content.res.Configuration;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
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 mDrawerLayout;
    private ActionBarDrawerToggle nav_DrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 設定側開式選單 側開選單是ActionBar的一個Action Item。
        // 需要下列三行
        ActionBar actBar = getSupportActionBar();
        actBar.setDisplayHomeAsUpEnabled(true);
        actBar.setHomeButtonEnabled(true);

        //drawerLayout整個螢幕畫面
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        //mActBarDrawerToggle側滑選單
        nav_DrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name);

        nav_DrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.addDrawerListener(nav_DrawerToggle);

    }
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        nav_DrawerToggle.syncState();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        nav_DrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (nav_DrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 晨柚 的頭像
    晨柚

    晨柚的部落格

    晨柚 發表在 痞客邦 留言(0) 人氣()