码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习:ActionBar活动条

时间:2018-03-17 10:52:46      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:gre   package   tool   err   通过   文本   蓝色   select   cal   

 

菜单定义:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="@string/font_size"
        android:showAsAction="always|withText"
        android:icon="@drawable/font">
        <menu>
            <!-- 定义一组单选菜单项 -->
            <group android:checkableBehavior="single">
                <!-- 定义多个菜单项 -->
                <item
                    android:id="@+id/font_10"
                    android:title="@string/font_10"/>
                <item
                    android:id="@+id/font_12"
                    android:title="@string/font_12"/>
                <item
                    android:id="@+id/font_14"
                    android:title="@string/font_14"/>
                <item
                    android:id="@+id/font_16"
                    android:title="@string/font_16"/>
                <item
                    android:id="@+id/font_18"
                    android:title="@string/font_18"/>
            </group>
        </menu>
    </item>
    <!-- 定义一个普通菜单项 -->
    <item android:id="@+id/plain_item"
          android:showAsAction="always|withText"
          android:title="@string/plain_item">
    </item>
    <item android:title="@string/font_color"
          android:showAsAction="always"
          android:icon="@drawable/color">
        <menu>
            <!-- 定义一组允许复选的菜单项 -->
            <group>
                <!-- 定义三个菜单项 -->
                <item
                    android:id="@+id/red_font"
                    android:title="@string/red_title"/>
                <item
                    android:id="@+id/green_font"
                    android:title="@string/green_title"/>
                <item
                    android:id="@+id/blue_font"
                    android:title="@string/blue_title"/>
            </group>
        </menu>
    </item>
</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/red"
            android:title="@string/red_title"
            android:alphabeticShortcut="r"/>
        <item
            android:id="@+id/green"
            android:title="@string/green_title"
            android:alphabeticShortcut="g"/>
        <item
            android:id="@+id/blue"
            android:title="@string/blue_title"
            android:alphabeticShortcut="b"/>
    </group>
</menu>

 

界面定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15pt"
        android:text="可通过上下文菜单修改背景色"
        />
</LinearLayout>

字符串资源:

<resources>
    <string name="app_name">ActionItemTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="red_title">红色</string>
    <string name="blue_title">蓝色</string>
    <string name="green_title">绿色</string>
    <string name="font_size">字体大小</string>
    <string name="font_color">字体颜色</string>
    <string name="plain_item">普通菜单项</string>
    <string name="font_10">10号字体</string>
    <string name="font_12">12号字体</string>
    <string name="font_14">14号字体</string>
    <string name="font_16">16号字体</string>
    <string name="font_18">18号字体</string>
</resources>

代码:

package allegro.actionbartes;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.txt);
        // 为文本框注册上下文菜单
        registerForContextMenu(txt);
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflator = new MenuInflater(this);
        //装填R.menu.my_menu对应的菜单,并添加到menu中
        inflator.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    // 创建上下文菜单时触发该方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View source,
                                    ContextMenu.ContextMenuInfo menuInfo)
    {
        MenuInflater inflator = new MenuInflater(this);
        //装填R.menu.context对应的菜单,并添加到menu中
        inflator.inflate(R.menu.context , menu);
        menu.setHeaderIcon(R.drawable.tools);
        menu.setHeaderTitle("请选择背景色");
    }
    // 上下文菜单中菜单项被单击时触发该方法
    @Override
    public boolean onContextItemSelected(MenuItem mi)
    {
        // 勾选该菜单项
        mi.setChecked(true);  //
        switch (mi.getItemId())
        {
            case R.id.red:
                mi.setChecked(true);
                txt.setBackgroundColor(Color.RED);
                break;
            case R.id.green:
                mi.setChecked(true);
                txt.setBackgroundColor(Color.GREEN);
                break;
            case R.id.blue:
                mi.setChecked(true);
                txt.setBackgroundColor(Color.BLUE);
                break;
        }
        return true;
    }
    @Override
    // 菜单项被单击后的回调方法
    public boolean onOptionsItemSelected(MenuItem mi)
    {
        if(mi.isCheckable())
        {
            // 勾选该菜单项
            mi.setChecked(true);  //
        }
        //判断单击的是哪个菜单项,并有针对性地作出响应
        switch (mi.getItemId())
        {
            case R.id.font_10:
                txt.setTextSize(10 * 2);
                break;
            case R.id.font_12:
                txt.setTextSize(12 * 2);
                break;
            case R.id.font_14:
                txt.setTextSize(14 * 2);
                break;
            case R.id.font_16:
                txt.setTextSize(16 * 2);
                break;
            case R.id.font_18:
                txt.setTextSize(18 * 2);
                break;
            case R.id.red_font:
                txt.setTextColor(Color.RED);
                mi.setChecked(true);
                break;
            case R.id.green_font:
                txt.setTextColor(Color.GREEN);
                mi.setChecked(true);
                break;
            case R.id.blue_font:
                txt.setTextColor(Color.BLUE);
                mi.setChecked(true);
                break;
            case R.id.plain_item:
                Toast toast = Toast.makeText(MainActivity.this
                        , "您单击了普通菜单项" , Toast.LENGTH_SHORT);
                toast.show();
                break;
        }
        return true;
    }
}

 

技术分享图片

 

Android学习:ActionBar活动条

标签:gre   package   tool   err   通过   文本   蓝色   select   cal   

原文地址:https://www.cnblogs.com/hackpig/p/8587260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!