【问题标题】:android: Why won't a textview stack over a button in a FrameLayoutandroid:为什么文本视图不会堆叠在 FrameLayout 中的按钮上
【发布时间】:2017-01-10 22:27:55
【问题描述】:

几乎每篇关于分层小部件主题的文章都指向 FrameLayouts,它们应该根据 XML 中的出现顺序进行堆叠。为了测试,我在xml中放了一个按钮和一个TextView,希望看到TextView与按钮重叠。按钮放在textview之前还是之后都没有关系,按钮总是在顶部。谁能看到我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jdot.testloadingfragments.MainActivity">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="        "
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="----------------------------------------------------------"/>


</FrameLayout>

【问题讨论】:

    标签: android android-framelayout


    【解决方案1】:

    elevation 添加到textView 将使其显示在Button 上方

    例如

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.jdot.testloadingfragments.MainActivity">
    
        <Button
            android:id="@+id/btn_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:text="        "/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:elevation="2dp"
            android:text="----------------------------------------------------------"/>
    </FrameLayout>
    

    更多信息在这里:Why is the framelayout not drawing z index correctly?

    【讨论】:

    • 谢谢。我正在避免提升,但不知道 Button 是一个特例。 AppCompat 的主题会迫使您对通用设计元素使用更新的 API 级别,这是没有意义的。
    【解决方案2】:

    FrameLayout 理想地用于显示单个项目。

    如果您想在框架布局中显示多个子视图,请尝试对每个子视图使用 android:layout_gravity 属性以正确定位它们而不重叠,如果您希望视图相互重叠,请使用高程。

    更多信息 访问https://developer.android.com/reference/android/widget/FrameLayout.html

    【讨论】:

    • 我知道提升,但我不能将我的应用程序限制为 API 21+
    • 尝试使用 app:elevation 属性来克服这个限制。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多