更新时间:2023-11-17 14:23:58
参考我的演示代码并进行调整:
Take reference to my demo code and adapt it:
package com.peterleow.layouts;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
public class RelativeLayoutsActivity extends Activity {
static final String[] alphabets = new String[] {
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, alphabets);
// create a RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// define the RelativeLayout layout parameters.
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// create a gridview
GridView gridView= new GridView(this);
gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
gridView.setNumColumns(4);
gridView.setAdapter(adapter);
// Adding the gridview to the RelativeLayout as a child
relativeLayout.addView(gridView);
// set the RelativeLayout as our content view
setContentView(relativeLayout, relativeLayoutParams);
}
}