如何隐藏布局下的按钮

How to hide buttons under layout

我有一个 RelativeLayout 作为父级 layout.In 它有两个按钮和一个 FrameLayout。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="button 1"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="button 2"
    android:layout_below="@+id/button1"/>

<FrameLayout
    android:background="@color/blue"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

FrameLayout 的宽度和高度都与 match_parent 一样,但我不明白为什么它没有覆盖按钮。

如何遮盖按钮?

将此添加到您的 Button 标签中:

<Button
...
android:background="@android:color/transparent" />

你可以把你的按钮放在里面 LinearLayout 它应该可以工作。

例如:-

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

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:text="button 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/button1"
        android:layout_margin="10dp"
        android:text="button 2" />
</LinearLayout>

试试这个代码,我已经测试过了。

<?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="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="button 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_margin="10dp"
        android:text="button 2" />

    </RelativeLayout>

    <FrameLayout
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</RelativeLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="button 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_margin="10dp"
        android:text="button 2" />

</RelativeLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue">

</FrameLayout>

现在代码可以运行了。

您在 主根布局 中添加了两个按钮,这将始终保持,使另一个视图成为相对视图或线性视图并将它们都放在其中,因此该视图重叠框架,如果它有匹配 parent。将其他视图保留在 Relative 布局中并分开 Framelayout,看看

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="null"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"

        android:text="button 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_margin="10dp"

        android:text="button 2" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/blue"
        android:minHeight="500dp">

    </FrameLayout>

</RelativeLayout> 

希望对您有所帮助!!

Layouts can hide a layout .. so to hide a button in the same layout you have to wrap up the buttons by another layout like this ..:-

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="button 1"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="button 2"

            android:layout_below="@+id/button1"/>
    </LinearLayout>

        <FrameLayout
            android:background="@color/blue"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </RelativeLayout>

按钮似乎有一个名为 stateListAnimator 的属性。

所以你要做的是为棒棒糖或更高版本制作另一个布局文件 (v21) 并将其添加到你的按钮中 xml:

android:stateListAnimator="@null"

对于较低版本的 Android,它似乎是这样工作的:

android:stateListAnimator="@null"
tools:ignore="UnusedAttribute"