【问题标题】:How to create button like AppCompat AlertDialog Button?如何创建像 AppCompat AlertDialog Button 这样的按钮?
【发布时间】:2015-07-15 17:45:42
【问题描述】:

我想覆盖系统窗口。

我正在尝试创建类似 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 样式(深色)的按钮。截图:

黑暗(作品):

黑暗焦点(作品):

光(作品):

光聚焦(不工作):

在这张图片中,我们可以看到一切正常。但是在手机中它看起来很丑。

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

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

【问题讨论】:

    标签: android android-appcompat


    【解决方案1】:

    我已经修好了。

    我将Button 替换为android.support.v7.widget.AppCompatButton

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

    现在它正在工作。

    【讨论】:

    • 请记住,在AppCompatActivity 中创建的每个Button 都会自动变为AppCompatButton
    • @NicolasTyler 是的,我知道。但在这种情况下,我将视图添加到系统窗口。我是在服务中做的。不过问题解决了。
    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多