【问题标题】:Android View doesn't become VISIBLEAndroid 视图不可见
【发布时间】:2023-03-05 20:40:01
【问题描述】:

我正在尝试为我的音乐播放器实现控件。

这是我的 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="true"
    android:fitsSystemWindows="true"
    app:behavior_hideable="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

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

        ...

            <LinearLayout
                android:id="@+id/control_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="bottom"
                android:orientation="horizontal"
                android:weightSum="4">

<!-- This is volume control Button -->
                <RelativeLayout
                    android:id="@+id/bottom_sheet_control_volume_container"
                    android:layout_width="wrap_content"
                    android:layout_height="56dp"
                    android:layout_weight="1"
                    android:gravity="center">

                    <ImageButton
                        android:id="@+id/bottom_sheet_control_volume"
                        android:layout_width="48dp"
                        android:layout_height="48dp"
                        android:background="@drawable/item_bg_transparent"
                        android:src="@drawable/ic_volume_up_black_24dp" />
                </RelativeLayout>

<!-- Other Buttons -->
                ...
            </LinearLayout>

<!-- Layout with volume control SeekBar -->
            <io.codetail.widget.RevealFrameLayout
                android:id="@+id/reveal_seek_bar_volume"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_alignParentBottom="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:visibility="visible">

                <LinearLayout
                    android:id="@+id/container_seek_bar_volume"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/white"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:visibility="gone">

                    <ImageButton
                        android:id="@+id/container_seek_bar_volume_close"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_marginLeft="16dp"
                        android:layout_marginStart="16dp"
                        android:background="@drawable/item_bg_transparent"
                        android:src="@drawable/ic_close_black_24dp" />

                    <android.support.v7.widget.AppCompatSeekBar
                        android:id="@+id/seek_bar_volume"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:layout_margin="16dp"
                        android:max="100"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingTop="0dp"
                        android:progress="100"
                        app:theme="@style/AppTheme.SeekBar.Accent" />
                </LinearLayout>
            </io.codetail.widget.RevealFrameLayout>

        </RelativeLayout>
    </LinearLayout>
</FrameLayout>

还有我的代码:

LLcontainerSBvolume = (LinearLayout) findViewById(R.id.container_seek_bar_volume);
IBcontrolVolume = (ImageButton) findViewById(R.id.bottom_sheet_control_volume);
        IBcontrolVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LLcontainerSBvolume.setVisibility(View.VISIBLE);
            }
        });

我还尝试为ImageButton 和其他Views 请求焦点:

IBcontrolVolume.requestFocus();
LLcontainerSBvolume.requestFocus();
LLcontainerSBvolume.requestLayout();

问题

问题来了:

当我启动我的Activity 时,点击歌曲,打开BottomSheet 并点击音量控制ImageButtonSeekbar 不会变成VISIBLE。我应该点击其他View,然后音量ImageButton 也可以。

【问题讨论】:

  • 你为什么要把整个container_seek_bar_volume视图GONE,而不是只应用到seek_bar_volume
  • @ShreeKrishna,是的,因为在SeekBar 附近也有接近的ImageButton
  • 您希望在开始时既不可见,又要务实地使可见。然后两者都可见还是只有一个可见?
  • @ShreeKrishna,我尝试将GONE 设置为ImageButtonSeekBar,并将INVISIBLE 设置为LLcontainerSBvolume 而不是GONE,效果很好。谢谢你的想法(:。但我仍然不明白制作ViewGroupGONE有什么问题...
  • @ShreeKrishna,也许你是对的。是的,当然,添加它。我会接受的。

标签: android layout view onclick visibility


【解决方案1】:

您在启动时将Visibility 设置为GONE 并使其在运行时可见的布局不在正式的android Layout 内部。它在自定义库io.codetail.widget.RevealFrameLayout 内部,在运行时更改Visibility 时可能存在错误。所以不要在ImageButtonAppCompatSeekBar 的容器中使用android:visibility。分开做就好了

<ImageButton
  -----
android:visibility="gone" />


<android.support.v7.widget.AppCompatSeekBar
   -----
android:visibility="gone" />

然后在onClick 中,按照您的要求,使两者都可见或可见

@Override
public void onClick(View v) {
   myImageButton.setVisibility(View.VISIBLE);
   myAppCompatSeekBar.setVisibility(View.VISIBLE);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2019-12-13
    相关资源
    最近更新 更多