0
我必須使用大量的 數據創建複雜的佈局(收據視圖)。因爲可以有x筆費用或付款,所以此視圖必須以編程方式完成。以編程方式創建佈局並添加到視圖
我想建立可重複使用的佈局,我可以隨員不同textview
S或edittext
S和讓他們排隊。
這可以歸結爲兩個基本的佈局:
- 全寬(其中文本居中)
- 分裂的列
a)其中一列佔據了3/4的左側或右側視圖。
b)第二列佔用1/4視圖。
這是我的嘗試。但我似乎沒有把它做對。 這是我的錯誤:
The specified child already has a parent. You must call removeView() on the child's parent first.
任何人都可以幫忙嗎?
private ScrollView mScrollView;
private LinearLayout mLinearLayout;
private ProgressBar mProgressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_payment, container, false);
mScrollView = (ScrollView) view.findViewById(R.id.svPayment);
mScrollView.setVisibility(View.GONE);
mProgressBar = (ProgressBar) view.findViewById(R.id.pbPayment);
mLinearLayout = (LinearLayout) view.findViewById(R.id.llPayment);
return view;
}
public void setupGUI() {
//RESUABLE LAYOUT
LinearLayout.LayoutParams paramsFull = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramsFull.setLayoutDirection(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams paramSmall = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramSmall.setLayoutDirection(LinearLayout.HORIZONTAL);
paramSmall.weight = 0.2f;
LinearLayout.LayoutParams paramLarge = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramLarge.setLayoutDirection(LinearLayout.HORIZONTAL);
paramLarge.weight = 0.8f;
// HOLDS THE LEFT AND RIGHT COLUMNS
LinearLayout columnShell = new LinearLayout(getContext());
columnShell.setLayoutParams(paramsFull);
//Small column
LinearLayout columnSmall = new LinearLayout(getContext());
columnSmall.setLayoutParams(paramSmall);
//Large column
LinearLayout columnLarge = new LinearLayout(getContext());
columnLarge.setLayoutParams(paramLarge);
//First get rid of all the views, incase of refresh
mLinearLayout.removeAllViews();
TextView textView = new TextView(getContext());
//CUSTOMER
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setLayoutParams(fullwidth);
textView.setText("Mary Jane");
columnShell.addView(textView);
mLinearLayout.addView(columnShell);
LinearLayout columnRight = new LinearLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
columnRight.setLayoutDirection(LinearLayout.HORIZONTAL);
//LEFT SIDE (1/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("PO: ");
columnSmall.addView(textView);
columnShell.addView(columnSmall);
//RIGHT SIDE (3/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("4465465456");
columnLarge.addView(textView);
columnShell.addView(columnLarge);
mLinearLayout.addView(columnShell);
}
}
fragment_payment.xml
<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"
android:orientation="vertical"
tools:context="com.mycompany.myapp.Views.MasterDetails.PaymentFragment">
<ProgressBar
android:id="@+id/pbPayment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="@+id/svPayment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/COLOR_LIGHT_GREY">
<LinearLayout
android:id="@+id/llPayment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</ScrollView>
</LinearLayout>
如果你沒有得到「正確的」,那麼告訴出了什麼問題......它會更好地使用'ListView'或'RecyclerView'(帶適配器) – snachmsm
看起來像使用'RecyclerView'可能會更好的方法 –
RecyclerView +功能來切換viewHolder基於你想要在列表中顯示的「View」。看看這個代碼路徑:https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView – MikeOscarEcho