【问题标题】:Why setVisibility doesn't work on a Fragment/FrameLayout为什么 setVisibility 在 Fragment/FrameLayout 上不起作用
【发布时间】:2014-10-06 19:08:18
【问题描述】:

我有一个带有不确定进度条的布局,经过 3 秒后,我想隐藏这个进度条并显示一个 webview,但由于某种原因它不起作用。

这是我的 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fl_register_oauth"
    tools:context=".fragments.RegisterFragment"
    android:background="@color/f_yellow">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity$PlaceholderFragment"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ll_pbar"
            android:gravity="center"
            android:visibility="gone">

        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateDrawable="@drawable/progress" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"
            android:visibility="gone"
            android:layout_gravity="center" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/FlashBarLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="72dp">
        <!-- flash bar content -->
    </FrameLayout>

</FrameLayout>

这是我的片段源代码。

public class RegisterFragment extends Fragment {

...

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register, container, false);

        bar = (ProgressBar) view.findViewById(R.id.progressBar);

        webView = (WebView) view.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });

我曾尝试使用带有 postDelayed 的处理程序来完成此操作,但我意识到它不起作用,即使我将其从处理程序中写出来。

bar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);

感谢您的帮助。

【问题讨论】:

  • 你看到进度条了吗?
  • 问题是我只看到进度条。我已经尝试使用 runOnUiThread 和带有 postDelayed 的处理程序,但进度条没有隐藏,webview 也没有显示。我不知道为什么! :(

标签: android android-fragments visibility android-framelayout


【解决方案1】:

试试:

((new Handler()).postDelayed(new Runnable() {
    @Override
    public void run() {
       bar.setVisibility(View.GONE);
       webView.setVisibility(View.VISIBLE);
    }
}, 3000);

您不需要使用 runOnUiThread。问题是:您的代码立即执行并且活动开始时没有显示进度条。

【讨论】:

  • 感谢您的评论,但正如我所说,我已经尝试过该方法,进度条不隐藏,webview 不显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 2019-11-18
  • 2012-01-31
  • 2020-10-31
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多