【发布时间】:2016-12-14 19:24:15
【问题描述】:
我必须使用大量 数据。因为可能有 x 笔费用或付款,所以这个视图必须以编程方式完成。
我正在尝试构建可重复使用的布局,我可以将它们附加到不同的 textviews 或 edittexts 并让它们对齐。
归结为两种基本布局:
- 全宽(文本居中)
- 拆分列
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>
【问题讨论】:
-
如果你没有得到“正确”然后告诉什么是错的......最好使用
ListView或RecyclerView(带适配器) -
看起来使用
RecyclerView可能是更好的方法 -
RecyclerView + 功能可根据您要在列表中显示的“视图”切换 viewHolder。查看此代码路径:guides.codepath.com/android/…
-
你给
mLinearLayout.addView(columnShell)打了两次电话。而且,正如大家所说,有更简单的方法可以做到这一点。 -
实际上,
ListView和RecyclerView对于少数项目来说可能都是矫枉过正。动态创建Views 并将它们添加到容器中没有任何问题。尤其是在项目数量相当少的情况下。
标签: android android-layout android-linearlayout android-scrollview