如何在使用 addView 时设置视图的位置

How to set position of view while using addView

我正在通过代码向 Layout 添加多个 View。我需要每个新的 View 都在前一个之上(父布局的顶部)。

编辑:为了更准确,我将描述应用程序模块应该做什么。用户从干净的屏幕和屏幕底部的一个按钮开始。该按钮在屏幕顶部添加一个 View。下一次点击应该在之前的视图之上添加下一个视图,以使最新的 View 位于容器的顶部。该应用程序保存状态并在重新启动时用户以相同的顺序查看视图。

确实需要您提供更多信息才能给出更准确的答案,但如果您说的和我想的一样,那么您只需将这些视图添加到方向设置为垂直的 LinearLayout 中即可。

并假设您正在遍历列表以动态添加视图,而不是从 0 开始递增,而是从列表的大小开始递减。

for(int i = size; i >= 0; i--){
   linearLayout.add(new TextView(Context));
}

如果您的意思是以编程方式添加一个视图,以便将新视图添加到前一个视图之上,而不是在它之下,那么我建议这样做:

  • 维护一个包含要转换为视图的项目的 ArrayList
  • 将它们放入 ListView
  • 当您想添加一个必须出现在列表顶部的新视图时,将其作为 ArrayList 的第一个元素插入并从中重新创建 ListView。

ViewViewGroup 中的位置由 LayoutParams

定义

这是怎么发生的?视图将它们的 LayoutParams 传递给它们的父 ViewGroups

    //100% programatic approach with simple LayoutParams
                LinearLayout myLinearLayout = new LinearLayout(this);

    //if the **parent** of the new linear layout is a FrameLayout 
                FrameLayout.LayoutParams layoutParams = 
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

                //or if you have the XML file you don't have to worry about this
                //myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);


                //you could have a LinkedList<TextView>
                LinkedList<TextView> textViewList = new LinkedList<>();


                //assuming the order is the correct order to be displayed
                Iterator<TextView> descendingIterator = textViewList.descendingIterator();

                while(descendingIterator.hasNext())
                {
                    //just add each TextView programatically to the ViewGroup
                    TextView tView = descendingIterator.next();
                    myLinearLayout.addView(tView);
                }

就像我们为 LinearLayout 定义 LayoutParams 一样,我们也可以为 TextView

定义 LayoutParams

重要提示:设置 LayoutParams 时,您需要确保它们 适合 VIEWGROUP,即所添加视图的 parent

private TextView textViewFactory(String myText) {


            TextView tView = new TextView(getBaseContext());

            //controling the position relatively to the PARENT
            //because you are adding the textview to a LINEAR LAYOUT
            LinearLayout.LayoutParams paramsExample =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);

            tView.setLayoutParams(paramsExample);


            //configuring the insides of the textview
            //you can also do all kinds of stuff programatically
            tView.setGravity(Gravity.CENTER_HORIZONTAL);

            tView.setPadding(10, 10, 10, 10);
            tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
            tView.setTypeface(Typeface.SANS_SERIF);
            tView.setTypeface(null, Typeface.ITALIC);
            tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
            tView.setId(R.id.aux_info);

            tView.setText(myText);
            //.........all kinds of stuff really

            return tView;
    }

Button 的 onClick 事件调用下面的方法。

private final int LAYOUT_TOP_INDEX = 0;

private void addViewOnTop(View view){
    if(layout != null && view !=null)
        layout.addView(view, LAYOUT_TOP_INDEX);
}

其中 'layout' 是要添加视图的布局(例如,LinearLayout)。