簡體   English   中英

Android:為什么在 SeekBar 上調用 viewToFront() 不會將其帶到 UI 頂部?

[英]Android: Why does calling viewToFront() on a SeekBar not bring it to the top of the UI?

我正在嘗試使隱藏的SeekBar (最初具有visibility = "gone" )可見並顯示在覆蓋整個屏幕的黑色背景 LinearLayout 之上。 但是,當我運行我的代碼時,我只看到全屏LinearLayout並且沒有看到位於LinearLayout之上的SeekBar

在此處輸入圖像描述

下面是我的代碼:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:orientation="vertical" >
    </LinearLayout>

    <SeekBar
        android:id="@+id/seekbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:padding="0dp"
        android:max="100"
        android:progressDrawable="@android:color/transparent"/>
</LinearLayout>

MainActivity.java

package com.example.changingimageviewwidth;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Make the image preview box visible
        SeekBar seekbar = findViewById(R.id.seekbar);
        seekbar.setVisibility(View.VISIBLE);
        seekbar.bringToFront();

        // Place the image preview box at a specified location
        seekbar.setX(150);
        seekbar.setY(150);

        seekbar.setVisibility(View.VISIBLE);
        seekbar.bringToFront();

        // Place the image preview box at a specified location
        seekbar.setX(150);
        seekbar.setY(150);
    }
}

這不能回答你的問題,但它可能會解決問題。 您可以使用ConstraintLayout來 position SeekBar 它是一個扁平的層次結構,因此您可以將視圖堆疊在一起,然后您可以使用visibility來隱藏/顯示它,而不是將其移動到位,它總是在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM