【问题标题】:Programmatically creating layouts and adding to view以编程方式创建布局并添加到视图
【发布时间】:2016-12-14 19:24:15
【问题描述】:

我必须使用大量 数据。因为可能有 x 笔费用或付款,所以这个视图必须以编程方式完成。

我正在尝试构建可重复使用的布局,我可以将它们附加到不同的 textviews 或 edittexts 并让它们对齐。

分解后看起来像这样:

归结为两种基本布局:

  1. 全宽(文本居中)
  2. 拆分列

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>

【问题讨论】:

  • 如果你没有得到“正确”然后告诉什么是错的......最好使用ListViewRecyclerView(带适配器)
  • 看起来使用RecyclerView 可能是更好的方法
  • RecyclerView + 功能可根据您要在列表中显示的“视图”切换 viewHolder。查看此代码路径:guides.codepath.com/android/…
  • 你给mLinearLayout.addView(columnShell)打了两次电话。而且,正如大家所说,有更简单的方法可以做到这一点。
  • 实际上,ListViewRecyclerView 对于少数项目来说可能都是矫枉过正。动态创建Views 并将它们添加到容器中没有任何问题。尤其是在项目数量相当少的情况下。

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


【解决方案1】:

为什么使用带有线性布局的滚动视图,您可能必须使用带有两个 textview 作为 chid 的列表视图,并且要添加新数据只需将其添加到列表中并通知适配器,您的新数据会自动添加到列表的最后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多