android java以编程方式在gridlayout中创建线性布局

android java以编程方式在gridlayout中创建线性布局,java,android,cardview,Java,Android,Cardview,我试图从API在gridlayout中创建cardview或linearlayout,所以我尝试使用java而不是xml创建它 我勉强实现了我想要的,但是当版面中的文本视图太长时,版面就会被破坏 以下是文本较短时的屏幕截图 这里是当文本太长的时候 这是我的密码 ViewGroup.LayoutParams params = new LinearLayout .LayoutParams(GridLayout.LayoutParams.MATCH_PARENT, GridLayout.L

我试图从API在gridlayout中创建cardview或linearlayout,所以我尝试使用java而不是xml创建它

我勉强实现了我想要的,但是当版面中的文本视图太长时,版面就会被破坏

以下是文本较短时的屏幕截图

这里是当文本太长的时候

这是我的密码

ViewGroup.LayoutParams params = new LinearLayout
    .LayoutParams(GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);
grid = v.findViewById(R.id.div_kategori);
grid.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
//grid.setLayoutParams(params);
grid.setColumnCount(3);
//grid.setRowCount(5);

int row = 1;
int col = 1;

for (int i=0; i<user.length(); i++) {
    JSONObject obj = user.getJSONObject(i);
    String nama_kategori = obj.getString("nama_kategori");

    if(i>0){
        if(i%3==0){
            row++;
            col = 1;
        }
    }

    Log.d("nama_kategori",nama_kategori+" baris : "+row+" kolom : "+col);

    LinearLayout card = new LinearLayout(getContext());
    GridLayout.LayoutParams param = new GridLayout
        .LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),      GridLayout.spec(GridLayout.UNDEFINED, 1f));
    card.setPadding(10,10,10,10);
    card.setBackgroundColor(Color.RED);
    card.setLayoutParams(param);

    FrameLayout frame = new FrameLayout(getContext());
    ViewGroup.LayoutParams paramframe = new LinearLayout
        .LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
    frame.setLayoutParams(paramframe);
    frame.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.card_edge) );

    LinearLayout lin = new LinearLayout(getContext());
    LinearLayout.LayoutParams paramlin = new LinearLayout
        .LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
    paramlin.setMargins(10,10,10,10);
    lin.setBackgroundColor(Color.BLUE);
    lin.setLayoutParams(paramlin);
    lin.setOrientation(LinearLayout.VERTICAL);

    TextView tv = new TextView(getContext());
    tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    tv.setText("sssssssssss sssss ssssss sssssss");
    tv.setTextColor(Color.BLACK);

    TextView tv2 = new TextView(getContext());
    tv2.setText("dua dua dua dua dua dua ");
    tv2.setTextColor(Color.BLACK);
    tv2.setMaxWidth(card.getWidth());

    lin.addView(tv);
    //lin.addView(tv2);
    frame.addView(lin);
    card.addView(frame);

    grid.addView(card);
    col++;
}
ViewGroup.LayoutParams params=new LinearLayout
.LayoutParams(GridLayout.LayoutParams.MATCH_父项,GridLayout.LayoutParams.WRAP_内容);
网格=v.findViewById(R.id.div_kategori);
grid.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
//grid.setLayoutParams(params);
grid.setColumnCount(3);
//grid.setRowCount(5);
int行=1;
int col=1;
对于(int i=0;i0){
如果(i%3==0){
行++;
col=1;
}
}
Log.d(“纳马·卡泰戈里”,纳马·卡泰戈里+”巴里斯:“+row+”科洛姆:“+col”);
LinearLayout卡=新的LinearLayout(getContext());
GridLayout.LayoutParams param=新的GridLayout
.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED,1f),GridLayout.spec(GridLayout.UNDEFINED,1f));
卡片。设置填充(10,10,10,10);
卡片.背景颜色(颜色.红色);
card.setLayoutParams(参数);
FrameLayout frame=新的FrameLayout(getContext());
ViewGroup.LayoutParams paramframe=新建线性布局
.LayoutParams(FrameLayout.LayoutParams.MATCH_父级,FrameLayout.LayoutParams.MATCH_父级);
frame.setLayoutParams(paramframe);
frame.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.card_edge));
LinearLayout lin=新的LinearLayout(getContext());
LinearLayout.LayoutParams paramlin=新的LinearLayout
.LayoutParams(FrameLayout.LayoutParams.MATCH_父级,FrameLayout.LayoutParams.MATCH_父级);
参数设置边距(10,10,10,10);
lin.setBackgroundColor(颜色:蓝色);
lin.setLayoutParams(paramlin);
林。设置方向(线性布局。垂直);
TextView tv=新的TextView(getContext());
tv.setTextAlignment(View.TEXT\u ALIGNMENT\u CENTER);
tv.setText(“SSSSSSSSS”);
tv.setTextColor(Color.BLACK);
TextView tv2=新的TextView(getContext());
tv2.setText(“dua dua dua dua dua dua”);
tv2.setTextColor(Color.BLACK);
tv2.setMaxWidth(card.getWidth());
林(电视),;
//lin.addView(tv2);
frame.addView(lin);
card.addView(框架);
grid.addView(卡片);
col++;
}

如果我了解您想要获得什么,问题应该是文本视图的宽度不受限制。 尝试设置ems并启用水平滚动。。比如:

tv.setEms(10);
tv.setHorizontallyScrolling(true);

要实现您想要的布局, 您可以在Recyclerview中使用GridLayoutManager

下面是链接

下面是同样的例子

最后,我可以用这个

GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
                            GridLayout.UNDEFINED,GridLayout.FILL,1f),
                            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
                    param.height = 0;
                    param.width = 0;
                    LinearLayout card = new LinearLayout(getContext());

谢谢,但它不起作用,结果还是一样