質問

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

}

}
役に立ちましたか?

解決

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top