我正在尝试定位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
这是它出现在我的模拟器上的方式
答案 0 :(得分:1)
还有另一种方法可以添加视图以及索引,在该位置添加与params
一起添加的位置addView(View child, int index, ViewGroup.LayoutParams params)
所以你可以使用它。 :)
答案 1 :(得分:0)
它对我有用,所以你只需检查视图的ID并确保正确添加布局参数。
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.view_below_id);
viewToLayout.setLayoutParams(layoutParams );
答案 2 :(得分:0)
将布局的父级更改为LinearLayout,方向设置为垂直,然后按照希望它们显示的顺序添加TextView和EditText。