API 11에서 제공하는 팝업 메뉴

메뉴에 서브메뉴가 가능해서 기존 노출되는 팝업 메뉴가 사라지고 서브메뉴가 나온다.


public void onPopupButtonClick(View button) {

    PopupMenu popup = new PopupMenu(this, button);

    popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());


    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        public boolean onMenuItemClick(MenuItem item) {

            Toast.makeText(PopupMenu1.this, "Clicked popup menu item " + item.getTitle(),

                    Toast.LENGTH_SHORT).show();

            return true;

        }

    });


    popup.show();

}


메뉴 xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2010 Google Inc.


     Licensed under the Apache License, Version 2.0 (the "License");

     you may not use this file except in compliance with the License.

     You may obtain a copy of the License at


          http://www.apache.org/licenses/LICENSE-2.0


     Unless required by applicable law or agreed to in writing, software

     distributed under the License is distributed on an "AS IS" BASIS,

     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

     See the License for the specific language governing permissions and

     limitations under the License.

-->

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

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

          android:icon="@android:drawable/ic_menu_search"

          android:title="@string/popup_menu_search" />

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

          android:icon="@android:drawable/ic_menu_add"

          android:title="@string/popup_menu_add" />

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

          android:icon="@android:drawable/ic_menu_edit"

          android:title="@string/popup_menu_edit">

        <menu>

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

                  android:icon="@android:drawable/ic_menu_share"

                  android:title="@string/popup_menu_share" />

        </menu>

    </item>

</menu>






'Android > ApiDemo_Graphic' 카테고리의 다른 글

View/Full Screen Modes / Hide Navigation  (0) 2012.12.29

화면에 동적으로 제어하는 코드로써
특징적인 것은 Navigation을 숨기는 코드가 들어 있다.

네비게이션을 옮기는 설정은 SYSTEM_UI_FLAG_HIDE_NAVIGATION인데, 아래와 같은 사항에 주의해서 사용해야 하네요.

SYSTEM_UI_FLAG_HIDE_NAVIGATION은  FLAG_FULLSCREEN와 함께 사용해야 제대로 동작된다고 합니다.

Android 4 treat: View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

"View has requested that the system navigation be temporarily hidden. This is an even less obtrusive state than that called for by SYSTEM_UI_FLAG_LOW_PROFILE; on devices that draw essential navigation controls (Home, Back, and the like) on screen, SYSTEM_UI_FLAG_HIDE_NAVIGATION will cause those to disappear. This is useful (in conjunction with the FLAG_FULLSCREEN and FLAG_LAYOUT_IN_SCREEN window flags) for displaying content using every last pixel on the display. There is a limitation: because navigation controls are so important, the least user interaction will cause them to reappear immediately."

This flag will allow to take over the entire screen, unlike Honeycomb where there was always a navigation bar at the bottom with room for the Back, Home, and Recents button. Unfortunately it won't work for games because of the last sentence.




/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.apis.R;

/**
 * This activity demonstrates some of the available ways to reduce the size or visual contrast of
 * the system decor, in order to better focus the user's attention or use available screen real
 * estate on the task at hand.
 */
public class OverscanActivity extends Activity {
    public static class IV extends ImageView {
        private OverscanActivity mActivity;
        public IV(Context context) {
            super(context);
        }
        public IV(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public void setActivity(OverscanActivity act) {
            mActivity = act;
        }
        public void onSizeChanged(int w, int h, int oldw, int oldh) {
            mActivity.refreshSizes();
        }
        public void onSystemUiVisibilityChanged(int visibility) {
            mActivity.getState().onSystemUiVisibilityChanged(visibility);
        }
    }

    private interface State {
        void apply();
        State next();
        void onSystemUiVisibilityChanged(int visibility);
    }
    private class NormalState implements State {
        public void apply() {
            display("Normal");
            setFullscreen(false);
            mImage.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
        public State next() {
            return new FullscreenState();
        }
        public void onSystemUiVisibilityChanged(int visibility) {
        }
    }
    private class FullscreenState implements State {
        public void apply() {
            display("FULLSCREEN");
            setFullscreen(true);
        }
        public State next() {
            return new FullscreenLightsOutState();
        }
        public void onSystemUiVisibilityChanged(int visibility) {
        }
    }
    private class FullscreenLightsOutState implements State {
        public void apply() {
            display("FULLSCREEN + LOW_PROFILE");
            mImage.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }
        public State next() {
            return new OverscanState();
        }
        public void onSystemUiVisibilityChanged(int visibility) {
        }
    }
    private class OverscanState implements State {
        public void apply() {
            display("FULLSCREEN + HIDE_NAVIGATION");
            mImage.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
        public State next() {
            return new NormalState();
        }
        public void onSystemUiVisibilityChanged(int visibility) {
        }
    }

    private void setFullscreen(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (on) {
            winParams.flags |=  bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    private String getDisplaySize() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        return String.format("DisplayMetrics = (%d x %d)", dm.widthPixels, dm.heightPixels);
    }
    private String getViewSize() {
        return String.format("View = (%d,%d - %d,%d)",
                mImage.getLeft(), mImage.getTop(),
                mImage.getRight(), mImage.getBottom());
    }
    void refreshSizes() {
        mText2.setText(getDisplaySize() + " " + getViewSize());
    }
    private void display(String text) {
        mText1.setText(text);
        refreshSizes();
    }
    State getState() {
        return mState;
    }

    static int TOAST_LENGTH = 500;
    IV mImage;
    TextView mText1, mText2;
    State mState;

    public OverscanActivity() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // we need to ask for LAYOUT_IN_SCREEN before the window decor appears
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        winParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        win.setAttributes(winParams);

        setContentView(R.layout.overscan);
        mImage = (IV) findViewById(R.id.image);
        mImage.setActivity(this);
        mText1 = (TextView) findViewById(R.id.text1);
        mText2 = (TextView) findViewById(R.id.text2);
    }

    @Override
    public void onAttachedToWindow() {
        mState = new NormalState();
        mState.apply();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void clicked(View v) {
        mState = mState.next();
        mState.apply();
    }
}














'Android > ApiDemo_Graphic' 카테고리의 다른 글

View PopupMenu  (0) 2012.12.29
런처 아이콘 사이즈 Android 2012. 12. 3. 15:13

Size and Format


Launcher icons should be 32-bit PNGs with an alpha channel for transparency. The finished launcher icon dimensions corresponding to a given generalized screen density are shown in the table below.

Table 1. Summary of finished launcher icon dimensions for each generalized screen density.

ldpi (120 dpi)
(Low density screen)
mdpi (160 dpi)
(Medium density screen)
hdpi (240 dpi)
(High density screen)
xhdpi (320 dpi)
(Extra-high density screen)
Launcher Icon Size36 x 36 px48 x 48 px72 x 72 px96 x 96 px

You can also include a few pixels of padding in launcher icons to maintain a consistent visual weight with adjacent icons. For example, a 96 x 96 pixel xhdpi launcher icon can contain a 88 x 88 pixel shape with 4 pixels on each side for padding. This padding can also be used to make room for a subtle drop shadow, which can help ensure that launcher icons are legible across on any background color.


http://developer.android.com/guide/practices/ui_guidelines/icon_design_launcher.html