对话框主题活动宽度

时间:2012-10-29 06:09:44

标签: android relativelayout

我有一个使用Dialog主题的活动。

<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);

3 个答案:

答案 0 :(得分:0)

事实证明,这不是因为Dialog主题,而是因为它是一个RelativeLayout而且它不知道onResume时的宽度。要解决此问题,请在设置视图后使用View.post()方法执行设置代码。在此处找到此解决方案android getHeight()/getWidth with RelativeLayout

答案 1 :(得分:0)

当您设置布局宽度FILL_PARENT时,您可以获得现在是您的视图宽度的屏幕尺寸

答案 2 :(得分:-2)

onCreate()方法之后,您可以获得高度和宽度。

相关问题