3

2 つの単純な解決策があるように見える単純な問題がありますが、どちらもうまくいかず、理由がわかりません。

縦向きのビューの背景と別の横向きの背景をレイアウトに使用したいと考えています。異なる画像を別々のフォルダlayoutとlayout-landにそれぞれ配置しました。

縦 = まさに本来あるべき姿 横 = 黒い画面

次に、drawable-land というフォルダーを作成し、そこにワイド ビューの背景を配置してみました。同じ結果です。

ポートレートに行くときは黒。

何か足りないものはありますか?これはとても単純なようで、何が間違っているのか理解できません。

前もって感謝します。

4

4 に答える 4

2

Orientation に基づく Drawable Image の処理は、追加のコードを実行することなく簡単です。いくつかの描画可能なディレクトリの命名構造に従い、それに応じて画像を配置するだけです。私は、このリンクが大いに役立つと思います: http://developer.android.com/guide/topics/resources/providing-resources.html

于 2015-03-17T09:50:57.360 に答える
1

chRyNan によって提供された受け入れられた回答をありがとう、それは何の問題もなく私にとってはうまくいきました。Constraint Layout が現在の Default Layout であるため、setBackGroundDrawable は非推奨です。2018年の別バージョンです。

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_organization_key_enter);

    ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.layout);
    Resources res = getResources();
    Drawable portrait = res.getDrawable(R.drawable.portrait);
    Drawable landscape = res.getDrawable(R.drawable.landscape);

    WindowManager window = (WindowManager)getSystemService(WINDOW_SERVICE);
    Display display = window.getDefaultDisplay();
    int num = display.getRotation();
    if (num == 0){
        constraintLayout.setBackground(portrait);
    }else if (num == 1 || num == 3){
        constraintLayout.setBackground(landscape);
    }else{
        constraintLayout.setBackground(portrait);
    }
于 2018-04-16T05:26:48.727 に答える
0

ファイルに異なるイメージが含まれていても、2 つのイメージ ファイルの名前が同じであることを確認してください。たとえば、drawable-land フォルダーと drawable-port フォルダーの両方に「background01.png」という名前の画像がある場合、次のように動作します。

   <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/background01" />
于 2014-11-17T15:23:00.250 に答える