2016年12月3日 星期六

Navigation Drawer 側邊選單教學 (PartIII)


前提回顧:

Navigation Drawer 側邊選單教學 (PartII)


上一篇教學主要是說明java程式撰寫讓Navigation選單動起來,下圖是在許多官方Google相關應用程式都

會看到的Navigation版面,看起來是不是很美觀呢 ! 滑動選單部分分成上方的Header以及下方menu item,

此篇教學接著上一篇內容教您如何美化版面



●下載icon

1.到Google design下載Icon下面有兩個推薦網站


2.可以關鍵字下載圖示(account、setting、exit)點選PNGS打包下載




















3.下載完成後解壓縮裡面可以看到有很多資料夾那是因為Android的icon圖示要搭配每個人手機的螢幕來分配大小,所以把資料夾裡的圖依序丟入project,如下圖所示


4.把下載下來對應的圖示放入Android專案資料夾中後並在navigation_menu.xml檔中加入icon的圖片來源id位置

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
    <item
android:id="@+id/nav_account"
       
android:title="My account"
       
android:icon="@mipmap/ic_person_black_24dp"/>
    <item
android:id="@+id/nav_settings"
       
android:title="Settings"
       
android:icon="@mipmap/ic_settings_black_24dp"/>
    <item
android:id="@+id/nav_logout"
       
android:title="Log Out"
       
android:icon="@mipmap/ic_exit_to_app_black_24dp"/>
</menu>

●建立標頭區塊Header

1.建立新的Layoutnavigation_header.xml 並且Root element:RelativeLayout


2.在navigation_header.xml中
Layout_hight高度140dp Padding:20dp和更改背景顏色 @color/colorPrimaryDark狀態列顏色
以及加入TextView大小20sp白色Padding20dp
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent" android:layout_height="140dp"
   
android:background="@color/colorPrimaryDark"
   
android:padding="20dp">

    <TextView
       
android:text="Header Navigation"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentTop="true"
       
android:layout_alignParentLeft="true"
       
android:layout_alignParentStart="true"
       
android:layout_marginTop="12dp"
        
android:id="@+id/textView2"
       
android:padding="10dp"
       
android:textColor="#ffffff"
       
android:textSize="20sp" />
</RelativeLayout>

3.接著在activity_main.xml加入引入headerLayout
app:headerLayout="@layout/navigation_header"

執行結果


到這邊先做一個段落,各位可能會發現側邊選單竟然被上方的ActionBar所侷限住了,我們經常所看到的選單長度是涵蓋整個螢幕的高,下一篇就會教各為如何自定義Toolbar來解決這個問題



Navigation Drawer 側邊選單教學 (PartII)

前提回顧:

Navigation Drawer 側邊選單教學 (PartI)


上一篇教學教導如何配置DrawerLayout版面和建立item選單,此範例接著上一個來實作左上角工具圖示   點下去顯示工具欄位


首先把activity_main.xml中的最外層的DrawerLayout加入id

android:id="@+id/drawerLayout"


●開始撰寫程式MainActivity.java

1.先在字串資源檔strings.xml建立兩個文字之後 ActionBarDrawerToggle建立物件時會使用到,它主要是function判斷選單滑開關閉若用字串"Open"、"Close"會有問題

<resources>

    <string name="app_name">NavigationApp</string>

    <string name="open">Open</string>

    <string name="close">Close</string>

</resources>

2.MainActivity.java中建立兩個私有全域變數

在此講解 mToggle.syncState()是隱藏箭頭 變成 ≡ 
getSupportActionBar().setDisplayHomeAsUpEnabled( true ) //為圖標決定左上角的圖標是否可以點擊因為Android4.0以後預設值為false

package com.example.andy6804tw.navigationapp;

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 mDrawerLayout;
    private
ActionBarDrawerToggle mToggle;

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

       
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawerLayout);
       
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);//必須用字串資源檔
       
mDrawerLayout.addDrawerListener(mToggle);//監聽選單按鈕是否被觸擊
       
mToggle.syncState();
//隱藏顯示箭頭返回
        // ActionBar 中的返回箭號置換成 Drawer 的三條線圖示。並且把這個觸發器指定給 layDrawer
        
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


   
}

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
if(mToggle.onOptionsItemSelected(item)){//當按下左上三條線或顯示工具列
           
return true;
       
}
       
return super.onOptionsItemSelected(item);
   
}
}

執行結果


現在程式能簡單的被觸發執行了並且使用者可以用手指向右滑動顯示選單,下一個教學要教各位如何優化版面,加入icon以及header標頭區塊

2016年12月2日 星期五

Navigation Drawer 側邊選單教學 (PartI)


前言:Navigation Drawer,通常是從左邊邊緣拉出來的一個導覽選單,是一個很常用到的 UI 元件。雖然Android Studio內建可以直接套做好的範本再做修改。但我們希望能搞懂運作關係此篇文章教您如何自定義設計一個自己的Toolbar+Navigation Drawer

首先進入教學前先來看成果 我們要實作的是側邊選單的功能,這個功能您可能會在Gmail、YouTube、Google Play......等Google開發的應用程式中看到此選單功能。

加入design資源再Sync同步

1.我們從Toolbar開始,先在build.gradle的dependencies大括號裡面加入
   後面的24.2.1版本對應您的appcompat-v7才能正確的被執行哦!

compile 'com.android.support:design:24.2.1'

在activity_main.xml中


1.更改activity_main.xml的介面排版選擇第一個DrawerLayout 並且刪除那些padding


2.再來加入NavigationView選取第一個


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"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.andy6804tw.navigationapp.MainActivity">



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent">



        <TextView

            android:text="MainLayout"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/textView"

            android:layout_weight="1"

            android:textAlignment="center"

            android:textSize="24sp" />

    </LinearLayout>

    <android.support.design.widget.NavigationView

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        app:menu="@menu/navigation_menu"

        android:layout_gravity="start">  //隱藏使用者可以像用滑動顯示工具列

    </android.support.design.widget.NavigationView>



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

建立menu

1.res建立menu資料夾並new一個新的navigation_menu.xml


2.建立item項目
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/nav_account"

        android:title="My account"/>

    <item android:id="@+id/nav_settings"

        android:title="Settings"/>

    <item android:id="@+id/nav_logout"

        android:title="Log Out"/>

</menu>


目前圖示



到目前為止已經完成部分版面配置(程式還不能跑)下一篇會教您如何動作側邊選單的程式撰寫

2016年10月15日 星期六

2016年10月13日 星期四

[C_AR190-易] 矩陣元素共同交集

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29925

這題就依序去尋找看看有沒交集的數,並丟入ArrayList中(因為最後要排序且不知容器有多大)
排序的語法是Collections.sort( )
注意在ITSA中ArrayList並不能簡略打法要遵循
ArrayList<Integer> list = new ArrayList<Integer> =>不能<>

2016年10月12日 星期三

[C_SO07-] 找出不合群的人

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2277

這題就按照題意找出outlier,注意題目範例輸出第二個有誤事排序後去比較另外每個資料只空一行而已
Q1的位置= n × 0.25
Q2的位置= n × 0.5
Q3的位置= n × 0.75

Input:
7.0 4.0 5.0 4.0 100.0 2.0 8.0 9.0 -100.0
Output:
The outlier is -100.0

The outlier is 100.0