我有一些布局,這些布局將被重復添加到主布局中:
我有一些布局,這些布局將被重復添加到主布局中。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"?
android:id="@ id/txt_temp_feels_like"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:fontFamily="sans-serif-medium"/span>
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="18sp" />
我希望它被添加到這個容器中:
<LinearLayout
android:id="@ id/temp_feels_like_container"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_marginStart="20dp"/span>
android:layout_marginEnd="20dp"。
android:layout_marginBottom="20dp"/span>
android:orientation="horizontal">
</LinearLayout>
而我想在代碼中做到這一點。我正在使用這種方法:
private fun setData (
布局。List<Int> 。
容器。List<ViewGroup>。
){
for (i in layout.indices) {
container[i].addView(layoutInflater.inflate(layout[i], null)
}
}
其中 layout 是指向 layout 資源的鏈接。
并且它已經完成了,但我有一些問題:
我有一些問題。
- 我不能為這個添加的視圖設定文本 。
- 我不能給這個視圖設定權重 。
也許我的演算法是錯誤的。你能幫忙嗎?
uj5u.com熱心網友回復:
像這樣的東西可以幫助嗎?
<LinearLayout
android:id="@ id/temp_feels_like_container"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_marginBottom="20dp"/span>
android:layout_marginEnd="20dp"/span>
android:layout_marginStart="20dp"/span>
android:orientation="水平"
>
<TextView
android:id="@ id/txt_temp_feels_like1"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:fontFamily="sans-serif-medium"/span>
android:visibility="消失"。
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="18sp"/span>
/>
<TextView
android:id="@ id/txt_temp_feels_like2"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:fontFamily="sans-serif-medium"/span>
android:visibility="消失"。
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="18sp"/span>
/>
<TextView
android:id="@ id/txt_temp_feels_like3"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:fontFamily="sans-serif-medium"/span>
android:visibility="消失"。
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="18sp"/span>
/>
<TextView
android:id="@ id/txt_temp_feels_like4"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_weight="1"/span>
android:fontFamily="sans-serif-medium"/span>
android:visibility="消失"。
android:text="-"/span>
android:textColor="@color/gray"/span>
android:textSize="18sp"/span>
/>
</LinearLayout>
你可以適當地設定重力(如果不是動態的,也可以設定文本)。 然后在代碼中設定可見性,而不是嘗試添加視圖。
findViewById<View> (R.id.txt_temp_feels_like1).visibility = View.VISIBLE
uj5u.com熱心網友回復:
這取決于你應該何時設定文本和重力。如果你能在添加到布局之前設定它--你可以做這樣的事情:
val view = layoutInflater.inflate(layout[i], null) as TextView
view.apply {
text = "abc"/span>
params = (params as LinearLayout.LayoutParams).apply {
weight = *你需要的值*。
}
}
container[i].addView(view)
或者,如果你需要在視圖被添加到布局后為視圖設定文本,你可以通過container.children/container.getChildAt(i)
UPD
你可以通過以下方式獲得孩子:
val count = layout.getChildCount()
var v: TextView? = null[/span
for (i in 0..count) {
val view = layout.getChildAt(i)
if (view is TextView) {
v = view
}
}
或者還有一種方法--在將標簽添加到布局中時將其設定為視圖,然后你可以在任何需要的時候通過標簽獲得它
。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328853.html
標籤:
