【问题标题】:Calling android dialog without it fading the background调用android对话框而不褪色背景
【发布时间】:2011-03-08 00:40:49
【问题描述】:

我有这个漂亮的对话框视图,我将 UserInputDialog 类设置为:

    <LinearLayout android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
        android:id="@+id/nameMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="What is your name Captain?"
        >
        </TextView>
        <EditText
        android:id="@+id/nameEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        >
        </EditText>
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal">
    <Button
        android:id="@+id/okButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK">
        </Button>
        <Button android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel">
        </Button>               
    </LinearLayout>
</LinearLayout>

我希望我的对话框出现,但背景不会淡出。这可能吗?因为调用此对话框的视图有​​一个整洁的背景,我希望将其显示为对话框的背景。

我在网上找到了这个:

<style name="doNotDim" parent="@android:style/Theme.Dialog">
    <item name="android:backgroundDimAmount">0</item>
</style >

但不确定如何将其应用于我的对话框?我有一门课叫public class UserInputDialog extends Dialog implements OnClickListener。它将其内容视图设置为上述布局。

我想我做得很好,只是不知道如何添加那种样式,所以我不能淡化背景。

第二个问题:您能否通过使用主题让您的对话框焕然一新(比如在其中显示带有文本的图像或图标)?

【问题讨论】:

    标签: android dialog views


    【解决方案1】:

    您还可以通过以下代码删除暗淡效果:

    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    

    【讨论】:

    • 这更简洁明了,应该是公认的答案。
    • 真的很有帮助。谢谢:)
    • 太棒了!而dialog.getWindow().setDimAmount(0.1f) 会有轻微的暗淡。值应在 [0.0f, 1.0f] 范围内。
    • 记得在对话框可用后执行此操作。我尝试了 onAttach 并没有用。在 onViewCreated 中它起作用了:)
    【解决方案2】:

    创建一个 res/values/styles.xml 文件并将其添加到其中。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="Theme.DoNotDim" parent="android:Theme">
        <item name="android:backgroundDimEnabled">false</item>
      </style>
    </resources>
    

    并将主题应用到您的活动中。

    <activity android:name=".SampleActivity" android:theme="@style/Theme.DoNotDim">
    

    【讨论】:

    • 这会使我从该活动中调用的每个对话框都使用相同的样式吗?
    • 其实这是 Activity 的样式设置,意味着 Activity 永远不会变暗。与对话框无关。
    • 自 api 3 起完美运行并得到支持
    【解决方案3】:

    创建自定义样式并将其放置在您的 values 文件夹中

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="YourCustomStyle" parent="android:Theme">
        <item name="android:backgroundDimEnabled">false</item>
      </style>
    </resources> 
    

    要为单个对话框设置样式,您可以使用

    Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
    

    【讨论】:

      【解决方案4】:

      要显示像 TrueCaller 这样的对话框,请执行以下操作:

      styles.xml 文件中。

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
        <style name="myBackgroundStyle" parent="android:Theme.Dialog">
          <item name="android:backgroundDimEnabled">false</item>
        </style>
      </resources>
      

      AndroidManifest.xml 中这样做:

      <activity android:name=".SampleActivity" android:theme="@style/myBackgroundStyle">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多