ListView listView = new ListView(context); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); params.setMargins(16, 16, 16, 16); params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); listView.setLayoutParams(params);
ListView listView = findViewById(R.id.listview); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 0, 1 ); params.setMargins(0, 16, 0, 16); listView.setLayoutParams(params);This code retrieves an existing list view from the layout XML file and sets its layout parameters to `MATCH_PARENT` for width and a weight of 1 for height. It also adds margins of 16 pixels on the top and bottom. The `android.widget` package library contains UI widgets and controls for building Android user interfaces.