LinearLayout getChildAt 在 Nougat 中的工作方式不同



我将这个布局作为 TableRow 的第一个子布局:

<LinearLayout style="@style/BodyAllegati"
android:layout_width="0dp"
android:layout_weight="0.65" 
android:paddingLeft="0dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/locGpsIcon"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:scaleType="center"
tools:ignore="ContentDescription"/>
<LinearLayout
style="@style/BodySegnalini"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="0dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>

我需要获得最内部的线性布局(ImageView 之后的布局)。

我通过以下方式做到了:

LinearLayout colAll = (LinearLayout) r.getChildAt(0); // outer l.layout (r is the TableRow)
LinearLayout colAllChild = (LinearLayout) colAll.getChildAt(0); // first inner l.layout
LinearLayout colSeg = (LinearLayout) colAllChild.getChildAt(1); // l.layout after the ImageView

碰巧在较旧的Android版本中都可以正常工作。如果我在Android 7中运行该应用程序,colSeg为空。

任何帮助将不胜感激。

我猜你有两个布局,一个在layout-v24中,你忘记在其中添加布局。 因此,您必须在两个布局中添加更改

最新更新