3

Android 3.2以降、複数の画面サイズをサポートするためのドキュメントを読むと、smallestScreenWidthDp条件付きでレイアウトを設定するために使用できますが、3.2より前のデバイスには何かありますか?

フラグメントベースのレイアウトがあり、画面サイズが600dpより大きい場合は、両方のフラグメントを画面に表示したいと思います。

これは、代替手段を見つけたいフラグメントを設定するために使用しているコードです。

public class MyActivity extends FragmentActivity  
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
            finish();
            return;
        }

        if (savedInstanceState == null) {
            final DetailFragment details = new DetailFragment();
            details.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}
4

2 に答える 2

2

これが私が使用するものです:

public static int getSmallestScreenWidthDp(Context context) {
    Resources resources = context.getResources();
    try {
        Field field = Configuration.class.getDeclaredField("smallestScreenWidthDp");
        return (Integer) field.get(resources.getConfiguration());
    } catch (Exception e) {
        // not perfect because reported screen size might not include status and button bars
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        int smallestScreenWidthPixels = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
        return Math.round(smallestScreenWidthPixels / displayMetrics.density);
    }
}

残念ながら、DisplayMetrics の画面サイズにはステータス バーやソフト ボタンが含まれていない可能性があるため、完全ではありません。

たとえば、Galaxy Tab 10.1 では、実際の値は 800 ですが、計算値は 752 しかありません。

于 2012-10-11T11:29:00.750 に答える
0

すでに述べたように、「SmallestScreenWidthDp」( http://developer.android.com/guide/practices/screens_support.html ) は 3.2 以降に最適です。3.2 より前のデバイスでは、構成オブジェクトをそのまま使用できました。

言い換えれば、いいえ(残念ながら)代替手段はありません...

于 2012-04-23T12:59:03.697 に答える