admin管理员组文章数量:1290982
I have a bunch of text views in a linear layout. The user's interaction with them is view-only: If the user guesses correctly (using voice), that guess will show in one of the text views. It looks like this (focus on the green boxes):
The pertinent XML looks like this:
<ScrollView
android:id="@+id/wof_guesses"
...
>
<LinearLayout
android:id="@+id/wof_guesses_list"
...
android:orientation="vertical">
<TextView
android:id="@+id/wof_guess0"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess1"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess2"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess3"
style="@style/WofGuess" />
...
</LinearLayout>
</ScrollView>
And this is the style XML:
<style name="WofGuess" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/mentor_grey_100</item>
<item name="android:text">Guess?</item>
<item name="android:fontFamily">@font/roboto</item>
<item name="android:visibility">invisible</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@android:drawable/editbox_background</item>
<item name="android:padding">16dp</item>
<item name="android:textSize">24sp</item>
</style>
In code, when the user guesses something, I try to set the text property of one of the text views (the next one still showing "Guess?") to the text the user just guessed. To get to those text views, I iterate through the children of the linear layout, like this:
for (tv in binding.wofGuessesList.children) {
tv.setBackgroundColor(Color.parseColor("#90EE90"))
tv.text = "no such property!"
tv.setText = "no such property!"
My question: There are dozens, if not hundreds, of properties, methods and callbacks available for those tv's, and I show here setting the background color to that green you see in the screenshot. But not the text??? What am I missing?
Thank you!
I have a bunch of text views in a linear layout. The user's interaction with them is view-only: If the user guesses correctly (using voice), that guess will show in one of the text views. It looks like this (focus on the green boxes):
The pertinent XML looks like this:
<ScrollView
android:id="@+id/wof_guesses"
...
>
<LinearLayout
android:id="@+id/wof_guesses_list"
...
android:orientation="vertical">
<TextView
android:id="@+id/wof_guess0"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess1"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess2"
style="@style/WofGuess" />
<TextView
android:id="@+id/wof_guess3"
style="@style/WofGuess" />
...
</LinearLayout>
</ScrollView>
And this is the style XML:
<style name="WofGuess" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/mentor_grey_100</item>
<item name="android:text">Guess?</item>
<item name="android:fontFamily">@font/roboto</item>
<item name="android:visibility">invisible</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@android:drawable/editbox_background</item>
<item name="android:padding">16dp</item>
<item name="android:textSize">24sp</item>
</style>
In code, when the user guesses something, I try to set the text property of one of the text views (the next one still showing "Guess?") to the text the user just guessed. To get to those text views, I iterate through the children of the linear layout, like this:
for (tv in binding.wofGuessesList.children) {
tv.setBackgroundColor(Color.parseColor("#90EE90"))
tv.text = "no such property!"
tv.setText = "no such property!"
My question: There are dozens, if not hundreds, of properties, methods and callbacks available for those tv's, and I show here setting the background color to that green you see in the screenshot. But not the text??? What am I missing?
Thank you!
Share Improve this question asked Feb 13 at 17:38 Yanay LehaviYanay Lehavi 2514 silver badges16 bronze badges 2 |1 Answer
Reset to default 1while you didnt provide a full example of what you are doing this code comes into suspect
for (tv in binding.wofGuessesList.children) {
tv.setBackgroundColor(Color.parseColor("#90EE90"))
tv.text = "no such property!"
tv.setText = "no such property!"
tv
here is likely View
and not a TextView
which has the .setText or .text
you are looking for. You need to cast tv as a TextView.
But also why are you looping through the children and not just reference them by their id's? You are creating unnecessary overhead
本文标签: Android Linear Layout Setting text view value from codeStack Overflow
版权声明:本文标题:Android: Linear Layout: Setting text view value from code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741512472a2382685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
View
toTextView
.(tv as? TextView)?.setText("no such property!")
. The type of tv here is typeView
. – OhhhThatVarun Commented Feb 13 at 17:48