如何在Android中覆盖UI元素

如何在Android中覆盖UI元素,android,Android,不久前,我为自己创建了一个非常简单的手电筒应用程序——一个按钮占据了整个屏幕,当点击它时,它会将背景从黑色变为白色(或再次变回白色),同时在“打开”/“白色”时也会使屏幕保持清醒。我现在正试图在应用程序底部添加一个seekbar来控制屏幕亮度,但我仍然希望整个屏幕显示黑色或白色,并且我希望seekbar始终可见。我正在使用约束布局 我试着在seekbar上将高程设置为2dp。seekbar最初是可见的,我可以调整它的值,但当我点击按钮时,它就消失了。到目前为止,在我的搜索中,我还没有找到任何其他

不久前,我为自己创建了一个非常简单的手电筒应用程序——一个按钮占据了整个屏幕,当点击它时,它会将背景从黑色变为白色(或再次变回白色),同时在“打开”/“白色”时也会使屏幕保持清醒。我现在正试图在应用程序底部添加一个seekbar来控制屏幕亮度,但我仍然希望整个屏幕显示黑色或白色,并且我希望seekbar始终可见。我正在使用约束布局

我试着在seekbar上将高程设置为2dp。seekbar最初是可见的,我可以调整它的值,但当我点击按钮时,它就消失了。到目前为止,在我的搜索中,我还没有找到任何其他方法来解决这个问题。有什么我忽略了的,或者有更好的方法去做吗

编辑:这是我的布局的XML。很抱歉没有早点回复

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/backgroundButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="changeBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:color/black" />

    <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="300dp"
            android:layout_height="20dp"
            android:elevation="2dp"
            android:layout_marginStart="64dp"
            android:layout_marginBottom="60dp"
            android:progressTint="#EE0323D1"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

试试这个:

public class TestActivity extends AppCompatActivity {

    boolean flashlightEnabled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        final View flashlight = findViewById(R.id.flashlight);
        flashlight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flashlightEnabled) {
                    v.setBackgroundColor(0xFF000000);
                } else {
                    v.setBackgroundColor(0xFFFFFFFF);
                }
                flashlightEnabled = !flashlightEnabled;
            }
        });
    }
}


您可以在这里发布您的XML吗?我们可以帮助你更好。[你好]谢谢你!我没有意识到这个布局可以用作按钮。这很有效!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/flashlight"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".TestActivity">

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>