Мне нужно создать сложный макет (вид чеков), используя множество данных . Потому что может быть x сумма платежей или платежей, это мнение должно быть сделано программно.Программно создавая макеты и добавляя к виду
Я пытаюсь построить многоразовые макеты, которые я могу Attache к различным textview
s или edittext
s и иметь их линию.
Сломался это выглядит примерно так:
сходящих 2 основных макеты:
- Полной ширина (где сосредоточен текст)
- раскол колонка
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