我正在尝试定位 TextViews、EditTexts 和一个按钮,但是它们都一直被挤在一起。它们都被动态添加到布局中,有什么方法可以将它们分别放置在 TextView 和 EditText 下面。如果可能的话,有人能指出我正确的方向。
这是我到目前为止所得到的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//putting the layout on xml page
View view = inflater.inflate(R.layout.fragment_two, container, false);
DisplayMetrics metrics = container.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int total;
width= width/6;
height = height/6;
//type of layout to be used
RelativeLayout relle = (RelativeLayout) view.findViewById(R.id.heloo);
int prevTextViewId = 0;
String [] array = getResources().getStringArray(R.array.income_page);
for (String anArray : array)
{
final TextView textView = new TextView(getActivity());
final EditText editText = new EditText(getActivity());
final Button submit = new Button(getActivity());
//setting text
textView.setText(anArray);
editText.setText(R.string.pound_sign);
submit.setText(R.string.submit);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
curTextViewId += 1;
editText.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
editText.setLayoutParams(params);
//textView.setTextColor(R.color)
prevTextViewId = curTextViewId;
relle.addView(textView, params);
relle.addView(editText, params);
relle.addView(submit, width, height);
}//for
return view;
}//view oncreate
这是它出现在我的模拟器上的方式