![](/img/trans.png)
[英]How can I add a textview to a linear layout with an onclick button listener?
[英]how do I add a custom layout to a Linear Layout on the click of a button
所以我正在嘗試創建一個任務應用程序,在其中,單擊一個按鈕,我希望我的子任務自定義視圖,其中包含一個文本視圖和 2 個圖像視圖,在水平線性布局內,顯示在垂直線性布局。 子任務所需的數據取自不同的活動,然后使用意圖傳輸。 此外,我需要添加不同的每個子任務的 id。 我不知道該怎么做。 請幫忙。 我無法為此編寫任何代碼。 我嘗試使用布局充氣器,在網上搜索了該怎么做,但我仍然不知道。 請幫忙。
您可以為此使用對話框片段,並且看起來也很有效讓我給您鏈接...對話框片段
我在我的項目中做了類似的事情,以編程方式在垂直線性布局中添加水平線性布局。
請檢查以下代碼:
View view = View.inflate(getActivity(), R.layout.item_horizontal_layout, null);
Button btnSave = view.findViewById(R.id.btnSaveModule);
layoutCount += 1; // A global value to store added layout counts
btnSave.setTag(layoutCount); // Set the tag on button so that we can use it later
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Access the layout position on button click
Log.d("Layout Position", ":" + (int)btnSave.getTag())
}
});
verticalLL.addView(view); // Add horizontal layout to vertical Linear Layout
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.