【问题标题】:Weird behavior of Button placing in Relative layout with AppCompatTheme使用 AppCompatTheme 在相对布局中放置按钮的奇怪行为
【发布时间】:2016-06-08 16:13:51
【问题描述】:

看代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">

    <Button
        android:id="@+id/wi_fi_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#feeca0"
        android:text="some text"
        android:textColor="#b5a24f" />

    <ProgressBar
        android:id="@+id/wifi_progressBar"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true" />
</RelativeLayout>

这种类型的元素放置,在将 UI 主题更新为 AppCompat 之前工作。更新后,所有按钮在所有情况下都重叠元素,即使它们必须是upper(根据xml代码)

这是我的 ActivityThem 风格

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:textAppearanceButton">@style/AppTheme.ButtonsTextAppearance</item>
    <item name="switchStyle">@style/SwitcherStyle</item>
</style>

如您所见,只有 textAppearance 覆盖。 Button(Base.Widget.AppCompat.Button) 的父组件没有任何特殊属性(我希望在 web ccs - z-index 中找到类似的东西)。 我解决了这个问题:只需用框架布局包装按钮,我的行为就会返回(进度条在按钮视图上绘制)。

那么,有没有人知道这是 AppCompat 中 Button 的错误或功能?

P.S:来自 AS 的屏幕截图,但在设备上的行为也相同。

【问题讨论】:

    标签: android android-appcompat android-theme android-styles


    【解决方案1】:

    您对 Z 指数的怀疑是正确的。 Android 5 引入了阴影和高程。 Button 具有默认高度 2dp,这使其呈现在具有较小高度(在相同布局中)的其他小部件之上,例如没有高度的 ProgressBar

    在运行 Android 4 及更早版本的设备上,海拔会被忽略。您的进度条将绘制在按钮上。

    最快和最肮脏的解决方法是将android:elevation="10dp" 分配给进度条。它不会绘制任何阴影,因为它没有任何背景。但它会比处于按下状态的按钮具有更大的高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2014-02-25
      • 2021-03-16
      • 1970-01-01
      相关资源
      最近更新 更多