1

ダイアログ テーマを使用するアクティビティがあります。

<activity android:name=".GamePreviewDialog" 
        android:theme="@android:style/Theme.Dialog"
        android:label="Chess Set Preview">

ダイアログの幅を取得する方法がわかりません。通常、ダイアログでない場合は、レイアウトで getWidth() を使用します。ただし、これでそれを行うとうまくいきません。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gamePreviewMainLayout"
    android:background="@color/game_background">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/gamePreviewButtonView"
        android:id="@+id/gamePreview"
        android:background="@color/game_background">
    </RelativeLayout>           
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:id="@+id/gamePreviewButtonView"
        android:background="@color/background">

        <Button 
            android:text="Close" 
            android:id="@+id/previewCloseBtn"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/chess_mates_btn">
        </Button>
    </LinearLayout>
</RelativeLayout>

contentView が他の投稿で提案されているように設定された後、これを呼び出してみました。プログラムで幅を取得する方法についてのアイデアはありますか?

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
4

3 に答える 3

0

レイアウト幅 FILL_PARENT を設定すると、現在のビュー幅である画面サイズを取得できます

于 2014-05-21T02:59:44.470 に答える
0

したがって、それはDialogテーマが原因ではなく、RelativeLayoutであり、onResumeの時点で幅がわからないためです。これを修正するには、View.post() メソッドを使用して、ビューのセットアップ後にセットアップ コードを実行します。ここでこのソリューションを見つけましたAndroid getHeight()/getWidth with RelativeLayout

于 2012-10-29T16:50:29.427 に答える
-2

メソッドの後onCreate()、高さと幅を取得できます。

于 2012-10-29T06:14:40.767 に答える