如何创建类似 AppCompat AlertDialog Button 的按钮?

How to create button like AppCompat AlertDialog Button?

我想叠加系统window。

我正在尝试创建类似于 AppCompat 样式的按钮。

我试过这个:

XML:

<LinearLayout 
    ...
    android:background="?android:attr/windowBackground"
/>

<!-- 
      My layout  ... 
      And button
      The buttonBarButtonStyle is in Theme.AppCompat and Theme.AppCompat.Light
 -->

 <Button 
      style="?attr/buttonBarButtonStyle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Close" />

服务:

LayoutInflater inflater;

public void setTheme(int theme)
{
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this, theme);
    inflater = LayoutInflater.from(this).cloneInContext(contextThemeWrapper);
}

public void onCreate(){
    boolean themeDark = getThemeDark();
    setTheme(themeDark ? R.style.Theme_AppCompat : R.style.Theme_AppCompat_Light);
    super.onCreate();
    createView(); // in this method i'm only inflating view using: inflater.inflate(layoutId, null); and adding view to window
}

它在 AppCompat 中工作,但在 AppCompat.Light 中不工作。我有 AppCompat 风格的按钮(深色)。截图:

深色(有效):

深色聚焦(有效):

灯光(有效):

光聚焦(不工作):

在这张图片中我们可以看到一切正常。然而在 phone 中它看起来很难看。

我想制作这个按钮(我在同一个应用程序中使用 android.support.v7.app.AlertDialog 创建了具有相同主题的 AlertDialog)并且它看起来不错:

我不知道为什么它不起作用。 我该怎么做?问题在于聚焦时按钮背景。我不知道为什么背景是黑暗风格的。 我试图设置 AlertDialog.AppCompat.Light 主题但没有用。

我已经修复了。

我用 android.support.v7.widget.AppCompatButton 替换了 Button

<android.support.v7.widget.AppCompatButton 
      style="?attr/buttonBarButtonStyle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Close" />

现在可以使用了。