6

環境

AlertDialog非常に単純な作業で、カスタム ビューを含むの高さを設定したいと考えています。このテーマに関する何十ものSOの質問を経て、12を思いつきました! 可能な答え。それらすべてを試した後 (1 秒あたりの試行で 15 年)、画面に頭をぶつけても同じ効果があるようです。私がそれをしたい理由は、私のカスタムビューには内在的な次元がなく、与えられたスペースにまたがっているだけだからです.


目標

ここでの質問はソフト ゴールに関するものですが、ハード ゴールの回答も喜んで受け入れます。

ソフト ゴール:AlertDialogカスタムの空のViewGroup(または を含む) を 作成しImageView、ダイアログの幅と高さを特定の値に設定します。

難しい目標:ダイアログのサイズがプログラムで指定されている場合に、可能な限り多くのスペースを占める正方形AlertDialogの表示を作成します。


私の試み、凝縮

ジャバ:

View layout = getLayoutInflater ().inflate (R.layout.alerttest, null);

AlertDialog alert = new AlertDialog.Builder (this)
    .setPositiveButton(R.string.alert_dialog_ok, null)
    .setTitle ("Don't cry, will you?")
    // .setView (layout) may be done here.
    .create();

alert.show ();

// alert.setView (layout) here, or before .show, or I can use the doc's:

FrameLayout fl = (FrameLayout) alert.findViewById (android.R.id.custom);
fl.addView (layout, new LayoutParams (500, 500)); // Arbitrary dimensions

alert.getWindow ().setLayout (800, 600);
// Or use before show (), or use alert.getWindow ().setAttributes with
// a WindowManager.LayoutParams crafted from getWindow ().getAttributes.
// Tried before and after show.

alerttest.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Tried with and without the encapsulating FrameLayout. -->
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <LinearLayout android:layout_width="500dp"
                  android:layout_height="500dp">
        <!-- Tried with a Space, an ImageView with a src. -->
        <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </LinearLayout>
</FrameLayout>

結果: (通常は正当な理由で) クラッシュしない場合は、幅のみが設定されます。高さは折りたたまれています。


関連する質問

どうもありがとう!

4

0 に答える 0