【问题标题】:Android-Flow Layout in JAVAJAVA中的Android-Flow布局
【发布时间】:2013-04-04 08:59:13
【问题描述】:

我在 JAVA 文件的 3 个文本视图中以水平方向给出 3 个句子,但只有 2 个文本视图,即只有 2 个句子,但第三个句子由于移动设备的分辨率而消失。我的问题是如何从第二行的起始位置而不是最后一行获取新行中的第三个文本视图。

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

}

}

【问题讨论】:

    标签: android flowlayout


    【解决方案1】:

    您看不到第三个TextView 的原因是您的布局具有水平方向,而前两个TextViews 适合屏幕尺寸,第三个被推到外面。

    要解决此问题,您可以执行几个步骤:

    1.在 XML 或 java 文件中将布局方向更改为垂直,这样TextView 将一个接一个地垂直显示。

    2.如果您想连续保留一个以上的TextView,那么您仍然应该将主布局方向设置为垂直,但对于TextView 的每一行创建使用代码的水平方向的新布局并将TextView 添加到此布局中。

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

    最后将此布局添加到您的主布局中:

    mailLayout.addView(tvRow);
    

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      相关资源
      最近更新 更多