我有一個TabWidget,裏面有一些片段。我想在其中顯示ProgressBar,但ProgressBar從不顯示。這裏我的代碼:ProgressBar Programaticaly in Fragment
private ProgressBar progressBar;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.roomstatusadmin_activity, container, false);
progressBar = new ProgressBar(getActivity().getApplicationContext(), null, android.R.attr.progressBarStyleSmall);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
//FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//layoutParams.gravity = Gravity.CENTER;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
progressBar.setLayoutParams(layoutParams);
return view;
}
我讀了一些問題,但沒有解決方案,如this。我測試與ProgressBar添加到我的layout.xml和ProgressBar是顯示。但我會在一些佈局中使用這個ProgressBar,所以我真的需要它programmaticaly。提前致謝。
刪除'getApplicationContext後添加到父佈局()' –