如何使用java在android运行时生成和添加gridview?
在这里,我创建了xml格式的gridview gridView1,并在其中显示静态数据。如何使用java在android运行时生成和添加gridview?,java,android,android-layout,android-gridview,Java,Android,Android Layout,Android Gridview,在这里,我创建了xml格式的gridview gridView1,并在其中显示静态数据。 Q.1是否可以在运行时以scrollviwe或线性布局生成gridview并将其添加到我的andoid应用程序中 Q.2如何更改gridview中显示的数据的字体大小 请帮忙 String[] mydata = new String[] { "Name", "Phone", "Mangesh", "63737377",
Q.1是否可以在运行时以scrollviwe或线性布局生成gridview并将其添加到我的andoid应用程序中 Q.2如何更改gridview中显示的数据的字体大小 请帮忙
String[] mydata = new String[] {
"Name", "Phone",
"Mangesh", "63737377",
"Rajnish", "63737344",
"Disha", "63737399",
"Ashwin", "63737312"};
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , mydata);
String[]mydata=新字符串[]{
“姓名”、“电话”,
“Mangesh”,“637377”,
“拉尼什”,“637344”,
“迪沙”,“637399”,
“Ashwin”、“63737312”};
gridView=(gridView)findViewById(R.id.gridView1);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,mydata);
谢谢您将声明您的布局mylayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainBack"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/transparent" >
<FrameLayout
android:id="@+id/gridViewFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
然后在代码中的适当位置调用一个方法以交换FrameLayout并用GridView替换它(不确定是否可以在其他位置执行此操作)
当然,您必须编写GridViewFragment
类。。。你只需在谷歌上搜索就可以了
但实际上,您必须创建一个类
GridViewFragment
(例如)来扩展Fragment
要做到这一点,您需要在Android中使用片段。您可以在我们的布局中放置占位符组件,并在创建活动时将其与片段交换。应该这样做。
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
private void attachGridViewFragment(int headerFrag) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
try {
if (findViewById(headerFrag) != null) {
Resources res = getResources();
String title = res.getString(R.string.app_icon_name);
xact.replace(headerFrag, new GridViewFragment(title, true, true), HEADER_FRAGMENT_TAG).commitAllowingStateLoss();
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
Log.e("MyActivityName", e.getMessage());
}
}