在android中自定义表格布局

问题描述 投票:1回答:1

我得到的布局

enter image description here

public class MainActivity extends AppCompatActivity {

    TableLayout tableLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tableLayout = findViewById(R.id.tableLayout);

        try{

            JSONObject jsonObject = new JSONObject(loadJson());
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            JSONArray jsonArray = jsonObject1.getJSONArray("application_step");

            JSONArray sections = jsonArray.getJSONObject(0).getJSONArray("section");
            for(int i = 0;i<sections.length();i++)
            {
                TableRow tr = new TableRow(this);
                JSONArray fields = sections.getJSONObject(i).getJSONArray("fields");
                for(int j = 0;j<fields.length();j++)
                {
                    Button button = new Button(this);
                    button.setText("Button"+j);
                    tr.addView(button);
                }
                tr.setFitsSystemWindows(true);
                //tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
                tableLayout.addView(tr);
            }

        }
        catch (Exception e){

            Log.d("ErrorTest ",e.getMessage());
        }

    }

    public String loadJson(){

        String json=null;

        try{
            InputStream io = MainActivity.this.getAssets().open("BaseUrlJson.json");
            int size = io.available();
            byte buffer[] = new byte[size];
            io.read(buffer);
            io.close();
            json = new String(buffer,"UTF-8");
        }
        catch (Exception e){
            Log.d("Error: ",e.getMessage());
            return null;
        }

        return json;
    }
}

上面的代码从JSON配置文件中获取布局。

我想通过在较短的行中扩展单元格(列方式)来使所有行具有相同的长度。

我正在动态生成它。

android android-tablelayout
1个回答
0
投票

我想你应该从这里检查这种适应性的布局。

https://github.com/Cleveroad/AdaptiveTableLayout

© www.soinside.com 2019 - 2024. All rights reserved.