以编程方式创建布局并添加到视图

时间:2016-12-14 19:24:15

标签: android android-layout android-linearlayout android-scrollview

我必须使用很多来创建复杂的布局(收据视图) 数据。由于可能有x笔费用或付款,因此必须以编程方式完成此视图。

enter image description here

我正在尝试构建可重复使用的布局,我可以将其附加到不同的textviewedittext s并让它们排成一行。

分解它看起来像这样: enter image description here

归结为2种基本布局:

  1. 整个宽度(文本居中的位置)
  2. 拆分栏
  3. a)其中一列占据左侧或右侧3/4的视图。

    b)并且第二列占据了视野的1/4。

    这是我的尝试。但我似乎没有做对。 这是我的错误:

      

    指定的孩子已经有父母。你必须调用removeView()   先关于孩子的父母。

    有人可以帮忙吗?

    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>
    

1 个答案:

答案 0 :(得分:0)

为什么要将scrollview与linearlayout一起使用,你可能必须使用带有两个textview的listview作为chid,并添加新数据只需将其添加到列表中并通知adpter,你的新数据会自动添加到列表的最后。