根据屏幕大小将Textviews添加到相对布局中

问题描述 投票:-1回答:1

我正在尝试在相对布局中动态创建文本视图。

问题:如果屏幕上有足够的空间,如何将它们添加到RIGHT_OF或如果空间不足,如何将它们添加到下面。

    void addTravelActivities()
    {
        RelativeLayout travel_act_layout=(RelativeLayout) findViewById(R.id.rl_for_travelact);

        for (int i=0;i<=15;i++)
        {
            TextView tv=new TextView(this);
            tv.setId(i);
            tv.setText(traveliconName.get(i));
            tv.setBackgroundResource(R.drawable.round_rect_shape);
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.plus_profile,0, 0, 0);
            tv.setCompoundDrawablePadding(10);
            tv.setTextSize(16f);
            tv.setTextColor(this.getResources().getColor(R.color.tripoto_orange));
            tv.measure(0, 0);

    //              tv.setBackground(this.getResources().getDrawable(R.drawable.round_rect_shape,null));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if(i==0)
            {
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.setMargins(8,0, 8, 8);
                travel_act_layout.addView(tv,params);
                /*tv.addOnLayoutChangeListener(new OnLayoutChangeListener() {

                    @Override
                    public void onLayoutChange(View v, int left, int top, int right,
                            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        // TODO Auto-generated method stub
                    int width=right-left;
                    Toast.makeText(NewProfileSettings.this,width+"", Toast.LENGTH_SHORT).show();
                    }
                });*/
            }
            else if(getWindowSize(tv.getMeasuredWidth())==true) {
            params.addRule(RelativeLayout.RIGHT_OF,i-1);
            params.setMargins(8,0, 8, 8);
            sum += tv.getMeasuredWidth();
            travel_act_layout.addView(tv,params);
    //              Toast.makeText(this, tv.getMeasuredWidth()+"",        Toast.LENGTH_SHORT).show();
            }
            else if(getWindowSize(tv.getMeasuredWidth())==false)
            {
                params.addRule(RelativeLayout.BELOW,i-1);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.setMargins(8,0,8,8);
                travel_act_layout.addView(tv,params);
            sum=0;
            }

        }

    }

    Boolean getWindowSize(int textViewWidth)
    {

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        int layout_width=width-40-textViewWidth;
        if(layout_width>sum)
        return true;
        else
            return false;

    }

我可能已经快要这样做了,因为我已经明白了这一点

Output

android textview dynamic-programming android-relativelayout
1个回答
0
投票

答:

是,流布局是@Frank N. Stein建议的答案。但是您会发现很多可以帮助您执行此操作的库。如果您不想使用库,这是我研究时发现的最简单的方法:

https://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/

只是一个类,您的布局进行了一些更改,并且可以根据需要运行。

© www.soinside.com 2019 - 2024. All rights reserved.