我有這樣的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:background="@color/white"/span>
android:orientation="垂直">
<TextView
android:id="@ id/txt_daytime"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:fontFamily="sans-serif-medium"。
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="16sp" />
<TextView
android:id="@ id/txt_temp"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:fontFamily="sans-serif-medium"。
android:text="-"/span>
android:textColor="@color/black"/span>
android:textSize="18sp" />
</LinearLayout>
我想把它添加到我的主布局的線性布局中:
<LinearLayout
android:id="@ id/temp_container"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_marginTop="20dp"/span>
android:layout_marginStart="20dp"/span>
android:layout_marginEnd="20dp"/span>
android:orientation="horizontal">
在代碼中。我在使用這個:
val dayTime = listOf(DayTime.Night, DayTime.MORNING, DayTime.DAY, DayTime.EVENING)
val tempContainer = binding.tempContainer
for (temp in dayTime) {
val weather = weatherData.getWeather(temp)
val view = LayoutInflater.from(requireContext()).inflate(R.layout.temp_layout, tempContainer) as LinearLayout
view.apply {
layoutParams = (layoutParams as LinearLayout.LayoutParams).apply {
weight = 1f
但是我如何在代碼中為我的重用布局中的這兩個TextViews設定值? 當我試圖在這個視圖中獲得孩子時,我不能選擇TextView,也不能給這個孩子設定值
。uj5u.com熱心網友回復:
((TextView) view.findViewById(R.id.txt_daytime)).setText("Custom1") 。
你也可以使用下面這段代碼
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)。)
TextView tv=new TextView(this)。
tv.setLayoutParams(lparams)。
tv.setText("test")。
this.m_vwJokeLayout.addView(tv)。
uj5u.com熱心網友回復:
在文本視圖上分配一個值是非常簡單的。
1 ) 你需要像這樣初始化textview:
val textView = findViewById(R.id.text_view_id) as TextView
2 ) 像這樣設定一個文本
textView.text = "你想設定的文本"
uj5u.com熱心網友回復:
為了讓你從布局中使用textView,你需要在你正在充氣的View上包含布局,或者將你想要訪問的textView放在正確的布局中,你可以用片段和/或活動來做到這一點。
你提供的這段代碼看起來像是你在線性布局中沒有任何id為 "temp_container "的textView,相反,你把它們放在另一個線性布局中。
要添加/初始化textView,你只需要
val txtDayTime = findViewById(R.id.txt_daytime) as TextView
或者
val txtTemp = findViewById(R.id.txt_temp) as TextView
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328695.html
標籤:
