選單選項OptionsMenu實現改變字型顏色和改變字型大小的功能
MainActivity的程式碼
package com.example.csdn1optionmenu;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
//建立選單選項optionMenu
//1.在res下建立menu目錄,建立xml檔案繼承memu,用來給選單選項佈局 res/menu/xml
// (這裡我們在選單裡新增兩個選項,更改字型大小/更改字型顏色為)
//2.要載入選單選項,就必須吃哦
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//先找到佈局檔案中要改變字型顏色或大小的文字內容
textView = (TextView) findViewById(R.id.text_view);
}
//要載入選單選項,就必須重寫onCreateOptionsMenu方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//得到選單載入器
MenuInflater menuInflater = getMenuInflater();
//將menu中的xml資原始檔中的選單選項載入到 menu物件上
menuInflater.inflate(R.menu.my_optionmenu, menu);
//return super.onCreateOptionsMenu(menu);
//返回值改成true
return true;
}
//為選單選項設定監聽器,重寫onOptionsItemSelected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//獲得顏色的id,分別是 紅/黃/藍/綠
int red = R.id.red;
int yello = R.id.yello;
int blue = R.id.blue;
int green = R.id.green;
//獲得 變大/變小 的id
int bigger = R.id.bigger;
int smaller = R.id.smaller;
//判斷點選的按鈕是哪個
//前四個是顏色的按鈕,後四個是變大變小的按鈕
if (item.getItemId() == red) {
//setTextColor:設定字型顏色的方法
// Color.RED:從父類Color中呼叫顏色的靜態常量值,用來設定字型的顏色
textView.setTextColor(Color.RED);
}
if (item.getItemId() == yello) {
textView.setTextColor(Color.YELLOW);
}
if (item.getItemId() == blue) {
textView.setTextColor(Color.BLUE);
}
if (item.getItemId() == green) {
textView.setTextColor(Color.GREEN);
}
//變大
if (item.getItemId() == bigger) {
//設定文字大小呼叫setTextSize方法,選擇含有兩個引數的
// unit:表示單位,通常用 PX ,用TypedValue方法呼叫,
// size:表示文字改變後的大小,textView.getTextSize()+5 表示在原有大小上 +5px
//textView.setTextSize(unit, size)
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getTextSize()+5);
}
//變小
if (item.getItemId() == smaller) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getTextSize()-5);
}
//返回值不用修改
return super.onOptionsItemSelected(item);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
主佈局中用來測試字型顏色和字型大小的佈局檔案
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<!-- 用來實現改變字型顏色和改變字型大小的文字框 -->
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這裡是選單選項OptionMenu的學習教程" />
</RelativeLayout>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在res/menu中用來增加選單選項(顏色/字型大小)的佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 在這裡給佈局新增選項
1.更改字型顏色:android:title 設定選項標題
這裡我們要實現點選進去有選擇顏色的子選項,就要在裡面繼續新增memu佈局
2.更改字型大小:更上面一樣
-->
<item
android:title="更改字型顏色" >
<menu >
<item android:title="紅色" android:id="@+id/red" />
<item android:title="黃色" android:id="@+id/yello" />
<item android:title="藍色" android:id="@+id/blue" />
<item android:title="綠色" android:id="@+id/green" />
</menu>
</item>
<item
android:title="更改字型大小" >
<menu >
<item android:title="變大" android:id="@+id/bigger" />
<item android:title="變小" android:id="@+id/smaller" />
</menu>
</item>
</menu>