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

时间:2016-09-29 16:16:51

标签: android android-layout android-view

我正在通过代码将多个View添加到Layout中。我需要每个新的View都高于前一个(父布局的顶部)。

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

4 个答案:

答案 0 :(得分:3)

按钮的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)。

答案 1 :(得分:1)

您真的需要更多信息来提供更准确的答案,但如果您说的是我认为您的意思,那么您只需将这些视图添加到方向设置为垂直的LinearLayout即可。

假设您正在迭代列表以动态添加视图,而不是从0递增,则从列表的大小向下递增。

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

答案 2 :(得分:1)

View内的

ViewGroup位置由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);
                }

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

定义LayoutParams

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

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;
    }

答案 3 :(得分:0)

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

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