【问题标题】:AppCompat Button Without Elevation没有高度的 AppCompat 按钮
【发布时间】:2015-11-29 07:50:56
【问题描述】:

我的应用中有几十个按钮,样式继承自 Widget.AppCompat.Button。大多数按钮都有一个高度和状态变化动画。太棒了。

令人不快的是,一些按钮没有任何额外的好处。

这是其中一个工作按钮的示例

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="?attr/defaultPadding"
    android:minHeight="?attr/minTouchSize"
    android:textAppearance="?attr/textAppearanceListRowLabel" />

这是一个按钮不起作用的 sn-p

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_weight="1"
        android:minHeight="?attr/minTouchSize" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="?attr/minTouchSize" />
</LinearLayout>

这些在不同的布局上。我尝试过的事情:

  • 我已将button1 添加到与按钮栏相同的布局文件中,button1 仍然有效。
  • 如果我从栏中的某个按钮中删除权重,它们将不起作用。
  • 我已经尝试将我的按钮样式明确地放在每个按钮上
  • 如果我将按钮栏设为垂直布局使按钮与父级匹配,那么它可以工作!但这不是我想要的。

我想让这些按钮在水平布局中具有状态动画和高度。又名,我希望按钮占据相等的空间并彼此相邻放置两个。我怎样才能做到这一点?

我正在使用 appcompat 22.2.1

【问题讨论】:

  • 您是否尝试在每个按钮上明确设置样式?
  • @Androider 不幸的是没有骰子:/
  • 我已经尝试删除 minHeight attrs 并将您的代码放入活动的主要布局中,并且效果很好。这些参数可能有问题。

标签: android android-button android-appcompat android-elevation


【解决方案1】:

我猜问题出在您的活动主题中!!!!

值得一提的是,按钮的风格来自 Activity THEME。 你可以这样做:

  1. 定义样式:

    <style name="ThemeMaterialButtonColor" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/md_teal_500</item></style>
    
  2. 然后在你的 xml 按钮中添加这个:

    app:theme="@style/ThemeMaterialButtonColor"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2021-03-14
    • 2015-08-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多