我的问题是,我正在尝试将自定义库视图添加到某些布局。我按照错误指定尝试了catRow1.removeAllViews();
,但它似乎无法正常工作。我做错了什么?
//填充视图的片段
monthlyLimit = (LinearLayout) getActivity().findViewById(R.id.monthlyLimit);
catRow1 = (LinearLayout) getActivity().findViewById(R.id.catRow1);
catRow2 = (LinearLayout) getActivity().findViewById(R.id.catRow2);
cat1 = (LinearLayout) catRow1.findViewById(R.id.cat1);
cat2 = (LinearLayout) catRow1.findViewById(R.id.cat2);
cat3 = (LinearLayout) catRow2.findViewById(R.id.cat3);
cat4 = (LinearLayout) catRow2.findViewById(R.id.cat4);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mPieChart = new DrawDonut(getActivity());
monthlyLimit.addView(mPieChart.getChart(), lp);
catRow1.removeAllViewsInLayout();
catRow1.removeAllViews();
cat1.addView(mPieChart.getChart(), lp);
添加的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" android:weightSum="10"
android:layout_height="match_parent" android:id="@+id/budgetLayout">
<LinearLayout android:id="@+id/monthlyLimit" android:layout_margin = "5dp"
android:layout_width="match_parent" android:orientation="vertical"
android:layout_height="0dp" android:layout_weight="4"/>
<LinearLayout android:id="@+id/catRow1"
android:orientation="horizontal" android:layout_margin = "5dp"
android:layout_width="match_parent" android:weightSum="2"
android:layout_height="0dp" android:layout_weight="3">
<LinearLayout android:id="@+id/cat1" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
<LinearLayout android:id="@+id/cat2" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout android:id="@+id/catRow2"
android:orientation="horizontal" android:layout_margin = "5dp"
android:layout_width="match_parent" android:weightSum="2"
android:layout_height="0dp" android:layout_weight="3">
<LinearLayout android:id="@+id/cat3" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
<LinearLayout android:id="@+id/cat4" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
答案 0 :(得分:1)
您可以在片段中的方法 onCreateView 中使用类LayoutInflater :
View inflate;
LinearLayout monthlyLimit;
LinearLayout catRow1;
LinearLayout catRow2;
LinearLayout cat1;
LinearLayout cat2;
LinearLayout cat3;
LinearLayout cat4;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inflate = inflater.inflate(R.layout.test_fragment, container, false);
monthlyLimit = (LinearLayout) inflate.findViewById(R.id.monthlyLimit);
catRow1 = (LinearLayout) inflate.findViewById(R.id.catRow1);
catRow2 = (LinearLayout) inflate.findViewById(R.id.catRow2);
cat1 = (LinearLayout) inflate.findViewById(R.id.cat1);
cat2 = (LinearLayout) inflate.findViewById(R.id.cat2);
cat3 = (LinearLayout) inflate.findViewById(R.id.cat3);
cat4 = (LinearLayout) inflate.findViewById(R.id.cat4);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mPieChart = new DrawDonut(getActivity());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
monthlyLimit.addView(mPieChart.getChart(), lp);
catRow1.removeAllViewsInLayout();
catRow1.removeAllViews();
cat1.addView(mPieChart.getChart(), lp);
}
});
return inflate;
}