Android-Flow Layout in JAVA

巧了我就是萌 提交于 2019-12-12 06:31:05

问题


I m giving 3 sentences in 3 text views in JAVA file in horizontal orientation but only 2 text view that is only 2 sentences are comin but the third sentence gets disappeared due to the resolution of the mobile. My query is how to get the third text view in a new line from the starting postion of the second line and not at the last.

public class MainActivity extends Activity {

private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById();
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 15, 10, 10);

    TextView tvTextsecond = new TextView(this);
    tvTextsecond.setText("Heywhatrudoingtoday");
    tvTextsecond.setLayoutParams(layoutParams);
    tvTextsecond.setBackgroundColor(Color.RED);
    tvTextsecond.setTextColor(Color.WHITE);
    //tvTextsecond.setSingleLine(true);
    layout.addView(tvTextsecond);

    TextView tvTextthird = new TextView(this);
    tvTextthird.setText("Haiitssundaytowork");
    tvTextthird.setLayoutParams(layoutParams);
    tvTextthird.setBackgroundColor(Color.BLUE);
    tvTextthird.setTextColor(Color.WHITE);
    //tvTextthird.setSingleLine(true);
    layout.addView(tvTextthird);

    TextView tvTextfourth = new TextView(this);
    tvTextfourth.setText("Owebullshitruuselessfellow");
    tvTextfourth.setLayoutParams(layoutParams);
    tvTextfourth.setBackgroundColor(Color.YELLOW);
    tvTextfourth.setTextColor(Color.WHITE);
    //tvTextfourth.setSingleLine(true);
    layout.addView(tvTextfourth);

}

private void findViewById() {
    layout = (LinearLayout) findViewById(R.id.flowLayout);

}

}

回答1:


The reason you don't see the third TextView is that your layout has a horizontal orientation and while the first two TextViews fit the screen size the third one is getting pushed outside.

To fix this issue you can do several steps:

1. change your layout orientation to vertical in the XML or the java file, and that way the TextView will appear one after the other vertically.

2. if you want to keep more then one TextView in a row, then you should still set you main layout orientation to vertical, but for each row of TextView's create a new layout with horizontal orientation using code and added the TextView to this layout.

LinearLayout tvRow = new LinearLayout();
tvRow.addView(firstTextView);
tvRow.addView(secondTextView);

Finally add this layout to your main layout:

mailLayout.addView(tvRow);


来源:https://stackoverflow.com/questions/15806706/android-flow-layout-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!