【发布时间】:2019-07-22 04:13:26
【问题描述】:
我得到的布局
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 配置文件中获取布局。
我希望通过扩展较短行中的单元格(按列)来使所有行的长度相同。
我是动态生成的。
【问题讨论】:
-
这听起来像一个网格。为什么不使用
RecyclerView和GridLayoutManager? -
不幸的是,我只能使用表格布局来实现它。
标签: android android-tablelayout