日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓动态增加按钮

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓动态增加按钮 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先了解一下LayouInflater類:將一個layout xml文件實例化為相應的View 對象。他永遠不會直接使用,而是使用 getLayoutInflater() 或者 getSystemService(String) 來獲得標準的LayoutInflater實例,這個實例已經將context連接起來,為正在運行的設備配置正確。比如:[java] view plaincopy LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); public View inflate (int resource, ViewGroup root)Since: API Level 1 Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error. Parametersresource ID for an XML layout resource to load (e.g., R.layout.main_page) root Optional view to be the parent of the generated hierarchy. ReturnsThe root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.假如有一個這樣的xml文件(guess_button.xml) 放入到一個buttonTableLayout:[html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/newGuessButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> (guess_button.xml)[html] view plaincopy <TableLayout android:id="@+id/buttonTableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:stretchColumns="0,1,2" > <TableRow android:id="@+id/tableRow0" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </TableRow> <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </TableRow> </TableLayout> (main.xml中定義的需要被擴充的buttonTableLayout,假設其在程序中的引用也叫buttonTableLayout, 即buttonTableLayout =(TableLayout) findViewById(R.id.buttonLayout);)現在我們將其第一行進行擴充:[java] view plaincopy LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); Button newGuessButton =(Button)inflater.inflater(R.layout.guess_button,null); //獲得按鈕 newGuessButton.setText("abc"); //設置按鈕文本 newGuessButton.setOnClickListener(guessButtonListener); //設置Listener (TableRow) buttonTableLayout.getChildAt(1); //將newGuessButton加到table第一行 OK 整個流程就完成了

總結

以上是生活随笔為你收集整理的安卓动态增加按钮的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。