Android的流式布局在JAVA [英] Android-Flow Layout in JAVA

查看:129
本文介绍了Android的流式布局在JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为给在水平方向的JAVA文件3文本视图3句,但只有2中只有2句是科曼,但第三句被消失,由于手机的分辨率文本视图。我的查询是如何得到从第二行的起点现在的位置是一个新行第三个文本视图,而不是在最后。

 公共类MainActivity延伸活动{私人的LinearLayout布局;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    findViewById();
    LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10,15,10,10);    TextView的tvTextsecond =新的TextView(本);
    tvTextsecond.setText(Heywhatrudoingtoday);
    tvTextsecond.setLayoutParams(的LayoutParams);
    tvTextsecond.setBackgroundColor(Color.RED);
    tvTextsecond.setTextColor(Color.WHITE);
    //tvTextsecond.setSingleLine(true);
    layout.addView(tvTextsecond);    TextView的tvTextthird =新的TextView(本);
    tvTextthird.setText(Haiitssundaytowork);
    tvTextthird.setLayoutParams(的LayoutParams);
    tvTextthird.setBackgroundColor(Color.BLUE);
    tvTextthird.setTextColor(Color.WHITE);
    //tvTextthird.setSingleLine(true);
    layout.addView(tvTextthird);    TextView的tvTextfourth =新的TextView(本);
    tvTextfourth.setText(Owebullshitruuselessfellow);
    tvTextfourth.setLayoutParams(的LayoutParams);
    tvTextfourth.setBackgroundColor(Color.YELLOW);
    tvTextfourth.setTextColor(Color.WHITE);
    //tvTextfourth.setSingleLine(true);
    layout.addView(tvTextfourth);}私人无效findViewById(){
    布局=(的LinearLayout)findViewById(R.id.flowLayout);}}


解决方案

看不到的原因,第三的TextView 是你的布局有一个水平方向的同时前两个的TextView 取值适合屏幕大小,第三个是越来越外推。

要解决这个问题,你可以做以下几个步骤:

1 更改布局方向垂直的XML或Java文件,这样在的TextView 将出现一前一后垂直。

2 如果你想保持一个以上的的TextView 成一排,那么你还是应该设置您的主要布局方向为垂直,但的TextView 的每一行的使用code创建水平方向的新布局,并添加了的TextView 到此布局。

 的LinearLayout tvRow =新的LinearLayout();
tvRow.addView(firstTextView);
tvRow.addView(secondTextView);

最后这个布局添加到您的主要布局:

  mailLayout.addView(tvRow);

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

这篇关于Android的流式布局在JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆