[英]How can I get a reference of a view from a inflated layout
I have a huge layout for my activity that i set using setContentView()
.我使用
setContentView()
为我的活动设置了一个巨大的布局。 In that layout I have a TableLayout
(named tableLayout
in my Activity) that I want to populate.在该布局中,我有一个要填充的
TableLayout
(在我的 Activity 中命名为tableLayout
)。 The rows of that TableLayout are customViews in a layout file(let's call that file tablerow_layout.xml
).该 TableLayout 的行是布局文件中的 customViews (让我们调用该文件
tablerow_layout.xml
)。 Inside this layout i have some TextViews and ImageView.在这个布局中,我有一些 TextViews 和 ImageView。 What I want to do is change the content of the TextView programatically, when I create the row.
我想要做的是在创建行时以编程方式更改 TextView 的内容。 This is my code:
这是我的代码:
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);
Unfortunately my app crashes when I try to change the content of the TextView.不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。 I know that I could do this using a ListView, but I usually have a small number of rows and the overhead of creating another Adapter for the ListView is preatty big.
我知道我可以使用 ListView 来做到这一点,但我通常只有少量行,并且为 ListView 创建另一个适配器的开销非常大。
As @ρяσѕρєя K suggested I also tried newRow.findViewById(...) instead of findViewById(...) without any difference正如@ρяσSYρєя K 建议我也尝试过 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别
How can I get a reference of a view from a inflated layout
如何从膨胀的布局中获取视图的引用
Use View
object which is returned from LayoutInflater.inflate
method to access views from layout which is passed as first parameter to inflate
method. 使用
View
,其从返回的对象LayoutInflater.inflate
方法来访问从布局的观点,其为第一个参数传递inflate
方法。
In your case, use newRow
object for accessing views from R.layout.tablerow_layout
layout: 在您的情况下,使用
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();
}
You are missing to set newRow
in your Textview . 您缺少在
newRow
中设置newRow 。
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
Using IntelliJ Amiya's and ρяσѕρєя K's answers it worked for me with this code. 使用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;
}
Each text had it's own i value. 每个文本都有自己的价值。 I believe you need to add the view first.
我相信你需要先添加视图。 Then you can change the values
然后您可以更改值
How to use the success view in the onCreate function.?如何在 onCreate 函数中使用成功视图。? Even I am not able to use the View object because createSuccessView() returns a view.
即使我无法使用 View 对象,因为 createSuccessView() 返回一个视图。
class TrendingActivity : AppCompatActivity() {类趋势活动: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.