0

我必须使用大量的 数据创建复杂的布局(收据视图)。因为可以有x笔费用或付款,所以此视图必须以编程方式完成。以编程方式创建布局并添加到视图

enter image description here

我想建立可重复使用的布局,我可以随员不同textview S或edittext S和让他们排队。

分布看,它看起来是这样的: enter image description here

这可以归结为两个基本的布局:

  1. 全宽(其中文本居中)
  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> 
+3

如果你没有得到“正确的”,那么告诉出了什么问题......它会更好地使用'ListView'或'RecyclerView'(带适配器) – snachmsm

+3

看起来像使用'RecyclerView'可能会更好的方法 –

+1

RecyclerView +功能来切换viewHolder基于你想要在列表中显示的“View”。看看这个代码路径:https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView – MikeOscarEcho

回答

0

为什么要使用滚动视图与LinearLayout中,您可能需要使用的ListView有两个TextView的为CHID,并增加新的数据只需将其添加到列表,并通知adpter,您的新数据会自动添加列表的最后一个。

相关问题