這是我第一次嘗試在我的 Android Studio 專案中實作一個 BottomSheetDialog。為了更熟悉這個程序,我嘗試在 Youtube 上遵循本教程:https : //youtu.be/hfoXhiMTc0c。在我的實際 Java 類中,當我掃描包含不同資訊的 NFC 芯片時,BottomSheet 被激活。但是,我無法在作業表上動態顯示來自芯片的資訊。我猜這是因為 Sheet 是靜態的?我如何能夠顯示來自芯片的資訊,該資訊已經存盤在我的 Java 類中的變數中,以顯示在 BottomSheet 的文本欄位中?
任何幫助表示贊賞,謝謝!
這是擴展BottomSheet的java類的代碼片段:
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
Scan.this, R.style.BottomSheetDialogTheme
);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
bottomSheetView.findViewById(R.id.addToCloset).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomSheetDialog.dismiss();
}
});
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();```
uj5u.com熱心網友回復:
我不熟悉BottomSheetDialog。但,
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
您應該能夠將上面的代碼替換為,
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
null
);
現在轉到 layout_bottom_sheet.xml 布局檔案。根據您的代碼,它應該具有沒有 id 的線性布局。給它一個id。我會將該 ID 視為“test_ll”。
現在你可以在上面的代碼下面,
LinearLayout ll = bottomSheetView.findViewById(R.id.test_ll);
之后,您可以將視圖動態添加到 ll。將視圖動態添加到 LinearLayout 參考,
將文本視圖添加到線性布局
編輯:
如果你想在 LinearLayout 中使用 Views,你可以這樣做,
View view = ll.findViewById(R.id.view_id);
如果你的文本視圖在 ll 中,
TextView textView1 = ll.findViewById(R.id.tvcolor);
textView1.setText("Hello!!");
這將解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359607.html
標籤:爪哇 安卓 xml android-bottomsheet对话框
上一篇:如何同時在swift中使用多個xmlparsers?
下一篇:XSL串列反向
