[英]How can I get a reference of a view from a inflated layout
我使用setContentView()
为我的活动设置了一个巨大的布局。 在该布局中,我有一个要填充的TableLayout
(在我的 Activity 中命名为tableLayout
)。 该 TableLayout 的行是布局文件中的 customViews (让我们调用该文件tablerow_layout.xml
)。 在这个布局中,我有一些 TextViews 和 ImageView。 我想要做的是在创建行时以编程方式更改 TextView 的内容。 这是我的代码:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow. findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);
不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。 我知道我可以使用 ListView 来做到这一点,但我通常只有少量行,并且为 ListView 创建另一个适配器的开销非常大。
正如@ρяσSYρєя K 建议我也尝试过 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别
如何从膨胀的布局中获取视图的引用
使用View
,其从返回的对象LayoutInflater.inflate
方法来访问从布局的观点,其为第一个参数传递inflate
方法。
在您的情况下,使用newRow
对象从R.layout.tablerow_layout
布局访问视图:
TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
try
{
LayoutInflater layoutInflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow.findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);
}catch(Exception e)
{
e.printTracktrace();
}
您缺少在newRow
中设置newRow 。
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
使用IntelliJ Amiya和ρяσѕρєяK的答案,它对我有用这个代码。
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout linearLayout =
(LinearLayout)findViewById(R.id.profileView);
ViewGroup viewGroup = linearLayout;
for (int i = 0; i < NUM_OF_PROFILES; i++){
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.profile_badge_layout, viewGroup, false);
linearLayout.addView(layout);
TextView names = (TextView)layout.findViewById(R.id.profileName);
ImageView images = (ImageView)layout.findViewById(R.id.profileImage);
names.setText(""+i);
viewGroup = layout;
}
每个文本都有自己的价值。 我相信你需要先添加视图。 然后您可以更改值
如何在 onCreate 函数中使用成功视图。? 即使我无法使用 View 对象,因为 createSuccessView() 返回一个视图。
类趋势活动:AppCompatActivity(){
private var currentState = UIState.loading
private lateinit var successView: View
private lateinit var loadingView: View
private lateinit var flContainer: ViewGroup
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_trending)
flContainer = findViewById<FrameLayout>(R.id.fl_layout_container)
loadingView = createLoadingView()
flContainer.addView(loadingView)
successView.layoutManager = LinearLayoutManager(this)
val newTrendingArrayList = ArrayList<TrendingModel>()
newTrendingArrayList.add(
0,
TrendingModel("octokit", "octokit.js")
)
val newTrendingAdapter = TrendingAdapter(newTrendingArrayList)
successView.adapter = newTrendingAdapter
val btnToggle = findViewById<Button>(R.id.btn_toggle)
btnToggle.setOnClickListener {
toggleView()
}
}
private fun createSuccessView(): View {
val view = layoutInflater.inflate(R.layout.trending_success, flContainer, false)
successView = view.findViewById(R.id.rv_trending)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.