我能夠操縱片段中現有TextView的一些文本。但是,我無法以編程方式將新的ProgressBar
添加到現有佈局。
片段中的類:以編程方式將進度條添加到Android中的片段中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_completed_office_hours, container, false);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayoutCompletedOfficeHours);
progressBar = new ProgressBar(this.getContext());
progressBar.setMax(daysInTotal);
progressBar.setProgress(daysCompleted);
linearLayout.addView(progressBar);
TextView textView = (TextView) view.findViewById(R.id.completedXOfYDays);
textView.setText(daysCompleted + "/" + daysInTotal);
return view;
}
的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"
tools:context=".fragment.CompletedOfficeHoursFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:id="@+id/linearLayoutCompletedOfficeHours">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/completedXOfYDays" />
</LinearLayout>
</FrameLayout>
當執行它,我得到19/299
文本,但沒有任何ProgressBar
。我在做什麼錯=
不幸的是,這並沒有解決它 –