Android:Theme.Dialog布局 - 动态可见内容被截止

时间:2011-04-05 18:33:46

标签: android android-layout

我有一个'对话'活动,基于内置的Android主题Theme.Dialog:

<style name="Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>        

    <item name="android:colorBackgroundCacheHint">@null</item>

    <item name="textAppearance">@android:style/TextAppearance</item>
    <item name="textAppearanceInverse">@android:style/TextAppearance.Inverse</item>

    <item name="textColorPrimary">@android:color/primary_text_dark</item>
    <item name="textColorSecondary">@android:color/secondary_text_dark</item>
    <item name="textColorTertiary">@android:color/tertiary_text_dark</item>
    <item name="textColorPrimaryInverse">@android:color/primary_text_light</item>
    <item name="textColorSecondaryInverse">@android:color/secondary_text_light</item>
    <item name="textColorTertiaryInverse">@android:color/tertiary_text_light</item>
    <item name="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item>
    <item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item>
    <item name="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item>
    <item name="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item>
    <item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item>
    <item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item>
    <item name="textColorHint">@android:color/hint_foreground_dark</item>
    <item name="textColorHintInverse">@android:color/hint_foreground_light</item>
    <item name="textColorSearchUrl">@android:color/search_url_text</item>

    <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
    <item name="textAppearanceMedium">@android:style/TextAppearance.Medium</item>
    <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
    <item name="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item>
    <item name="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item>
    <item name="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item>
</style>

我的Activity的布局,我有一个EditText,一个Button和一个不可见的TextView,用于根据EditText内容的有效性向用户显示消息:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/dialog" 
android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:paddingBottom="6dp">
    <TextView 
        style="@style/DialogMessage"
        android:id="@+id/error" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:visiblity="gone" />
    <EditText 
        android:id="@+id/title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text|textCapWords"
        android:maxLength="100"
        android:maxLines="1"
        android:ems="20"
        android:scrollHorizontally="true" />
    <Button 
        android:id="@+id/rename"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" />    
</LinearLayout>

当用户处于纵向模式,并且他们在EditText中输入一些“坏”文本时,我使TextView可见,并用适当的错误消息填充它。这样可以正常工作,因为一切都会向下移动,即使内容跨越多行,TextView也会显示得很好。现在,在横向中,当我这样做时,TextView显示正常,但底部元素(在本例中为Button)被压缩到大约正常高度的一半。移动元素,如果我将EditText放在底部,当TextView可见时,它也被压缩,并且在底部放置TextView会导致除了单行文本之外的任何内容。

我尝试通过以下方式重新设置LinearLayout的大小来填充TextView之后强制解决问题:

LinearLayout dialog = (LinearLayout) findViewById(R.id.dialog);
dialog.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

我已经尝试通过调用:

填充TextView后重新绘制LinearLayout
LinearLayout dialog = (LinearLayout) findViewById(R.id.dialog);
dialog.invalidate();

都没有奏效。任何想法在这里发生了什么?

谢谢,

编辑:

有足够的屏幕空间可以完全显示所有三个组件...作为一个完全膨胀的TextView,其内容与我动态膨胀相同,并且所有内容都完全显示。

1 个答案:

答案 0 :(得分:8)

没有弄清楚为什么会这样,但想出了解决办法。将布局切换为RelativeLayout解决了这个问题,现在使用以下内容正确显示所有内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/dialog" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp">
<EditText 
    android:id="@+id/inpt_title" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text|textCapWords"
    android:maxLength="100"
    android:maxLines="1"
    android:ems="20"
    android:scrollHorizontally="true" />
<Button 
    android:id="@+id/btn_rename"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@id/inpt_title"
    android:layout_below="@id/inpt_title" />  
<TextView 
    style="@style/DialogMessage"
    android:id="@+id/error" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn_rename"
    android:layout_alignLeft="@id/inpt_title"
    android:layout_alignRight="@id/inpt_title" />  
</RelativeLayout>