Android 中一种判断 Navigation Bar 位置的方法

  public boolean isNavigationBottom() {
        WindowManager wm;
        Display display;
        DisplayMetrics realSize;
        DisplayMetrics windowSize;

        realSize = new DisplayMetrics();
        windowSize = new DisplayMetrics();
        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        display = wm.getDefaultDisplay();

        display.getRealMetrics(realSize);
        display.getMetrics(windowSize);

        if (windowSize.heightPixels != realSize.heightPixels)
            return true;
        else
            return false;
    }

getRealMetrics()getRealSize()的功能几乎一样。

see also: get physical screen size

PHP 中 extract 同名覆盖

extract()方法会根据一个Array创建相应的变量,并且会覆盖同名变量的值。幸运的是extract()提供了第二个参数,一个标志符。

EXTR_OVERWRITE If there is a collision, overwrite the existing variable.

EXTR_SKIP If there is a collision, don’t overwrite the existing variable.

EXTR_PREFIX_SAME If there is a collision, prefix the variable name with prefix.

EXTR_PREFIX_ALL Prefix all variable names with prefix.

EXTR_PREFIX_INVALID Only prefix invalid/numeric variable names with prefix.

EXTR_IF_EXISTS Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example.

EXTR_PREFIX_IF_EXISTS Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.

EXTR_REFS Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of the array parameter. You can use this flag on its own or combine it with any other flag by OR’ing the flags.

If flags is not specified, it is assumed to be EXTR_OVERWRITE.

所以解决同名覆盖的一个方法就是使用EXTRA_PREFEX_ALL标志,这样extract()在创建Array对应变量的时候会自动加上第三个参数设置的前缀字符串。eg:

        $name = "wangsijie";
        $person = array('name' => "lnmcc", 'age' => 100);
        extract($person, EXTR_PREFIX_ALL, "person");
        echo "Name: {$name}, Person Name: {$person_name}, Person Age: {$person_age}";  

关于图片 Drawable 的宽高

在Android项目的res目录下有一些对应不同屏幕DPI的子文件夹: Alt none 分别对应的DPI为:

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi

为什么要分这些文件夹呢?

为了能够自动适配不同的屏幕,使他们看上去大小(基本)一致。

我们可以使用一张分辨率为500×500的图片(test.jpg)做一些测试。

测试代码片段如下:

public class MainActivity extends Activity {

	TextView tv;
	ImageView img;
	Drawable drawable;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		img = (ImageView)findViewById(R.id.img);
		tv = (TextView)findViewById(R.id.tv);
		drawable = getResources().getDrawable(R.drawable.test);
		String text = "IntrinsicHeight: " + drawable.getIntrinsicHeight() + " IntrinsicWidth: " + drawable.getIntrinsicWidth();
		tv.setText(text);
		img.setImageDrawable(drawable);
	}
}

先把test.jpg拷贝到xxhdpi下,在Nexus 5 (480DPI)下显示的IntrinsicHeight和IntrinsicWidth为500×500; 而在Nexus 7 (320DPI)下显示的IntrinsicHeight和IntrinsicWidth为333×333 。

接着我们把test.jpg换到xhdpi下,在Nexus 5(480DPI)下显示的IntrinsicHeight和IntrinsicWidth为750×750; 而在Nexus 7 (320DPI)下显示的IntrinsicHeight和IntrinsicWidth为500×500 。

为什么一张相同图片在不同的设备上显示出了不同的分辨率呢?这个不同的分辨率跟具体的设备有什么关系么?

答案是跟你设备的DPI有关。Android为了显示效果不依赖设备像素(pix),特别捣鼓出了一个设备独立像素(dp)。具体可参考Supporting Multiple Screens

所以在上面的例子中有:

Drawable的宽高 = 实际图片的宽高 * (设备的DPI / 文件夹对应的DPI)

Cocoa应用程序执行流程

Alt none

Android Preference默认值设置

在XML文件(advanced_preferences.xml)中为Preference设置默认值

<!-- default value is a boolean -->
<CheckBoxPreference
    android:defaultValue="true"
    ... />

<!-- default value is a string -->
<ListPreference
    android:defaultValue="@string/pref_syncConnectionTypes_default"
    ... />

在代码中还需要调用

PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

第三个参数接受一个BOOL值,含义如下:

true:每次调用setDefaultValues都会导致Preference的初始化。
false:只初始化一次,重复调用setDefaultValues不会导致重复初始化。

PreferenceManager.setDefaultValues() This method takes three arguments:

  • Your application Context.

  • The resource ID for the preference XML file for which you want to set the default values.

  • A boolean indicating whether the default values should be set more than once.

  • When false, the system sets the default values only if this method has never been called in the past (or the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false).

As long as you set the third argument to false, you can safely call this method every time your activity starts without overriding the user’s saved preferences by resetting them to the defaults. However, if you set it to true, you will override any previous values with the defaults.