Progressbar in ActionBar

2019-04-10 18:16发布

I am working with an app which will load a website Inside the app .Now i want to add a ProgressBar in action bar without swipe up to repress feature.

Like that

enter image description here

I am using Fragment in my app.

WebviewFragment

public class WebviewFragment extends Fragment {

    WebView webView;
    String newslink;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           View view = inflater.inflate(R.layout.fragment_webview, container, false);

        newslink = getArguments().getString("LINK");


        webView = (WebView) view.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(newslink);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }
                return false;
            }
        });


        return view;

    }


}

fragment_webview.XML

<?xml version="1.0" encoding ="utf-8"?
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >




        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView" />


</LinearLayout>

Any solution for me ?

1条回答
Evening l夕情丶
2楼-- · 2019-04-10 18:56

if you want add progress to actionbar call this method before setlayout

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

UPDATE where should request window feature

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);
     .... // other code goes here 
       }

and call this method when you want run it

EDIT in fragment you can add getActivity() before requestWindowFeature to be getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

 setProgressBarIndeterminateVisibility(true); // turn progress on

     setProgressBarIndeterminateVisibility(false); // turn progress off

hope this work , my recommendation to use toolbar

<android.support.v7.widget.Toolbar     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_green_500"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<ProgressBar
    android:id="@+id/toolbar_progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateTint="#795548"
    android:indeterminateTintMode="src_in"
    android:layout_gravity="right"
/>

</android.support.v7.widget.Toolbar>

Why I recommended toolbar with ProgressBar ?

for two reasons , First setSupportProgressBarIndeterminate deprecated.

Second easy to use just set setVisibility method to make it visible call setVisibility(View.VISIBLE); to hide view call setVisibility(View.GONE);

查看更多
登录 后发表回答