![](/img/trans.png)
[英]Is it possible to make an android keyboard entirely out of Java without using xml
[英]In Android Programming, How to make a GridView in Java without using XML?
我想用Java創建一個GridView ,我不想使用XML。 下面的示例是XML作為布局。 它顯示3列。 我只想使用Java來實現該設置。
在我的活動中,而不是GridView gv = (GridView) findViewById (R.id.gridview);
我使用GridView gv = new GridView (this);
。
現在我唯一的問題是它只有1列 。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
相當於android:numColumns
的代碼是setNumColumns(int)
。 對於諸如id之類的事情,您需要返回“查看文檔”。 寬度和高度將通過LayoutParams設置。
Android Developer.Com上的GridView文檔
嘗試這個。
GridView gv = new GridView(context);
gv.setId(999999);
gv.setLayoutParams(new
GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
gv.setBackgroundColor(Color.GREEN);
gv.setNumColumns(3);
gv.setColumnWidth(GridView.AUTO_FIT);
gv.setVerticalSpacing(5);
gv.setHorizontalSpacing(5);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setGravity(Gravity.CENTER);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.