如何在android programaticall中创建gridview [英] how to create gridview in android programaticall
本文介绍了如何在android programaticall中创建gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用java代码动态创建gridview用于开发Android应用程序我使用相对布局请帮助我asap
how to create gridview dynamically using java code for developing android application i am using Relative Layout please help me asap
推荐答案
参考我的演示代码并进行调整:
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);
}
}
这篇关于如何在android programaticall中创建gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文