我做了一个线性布局,我有一个视图和一个按钮。在线性布局中,我还添加了一些元素,但是以编程方式。这是代码:
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/transparentBlack1"/>
<Button
android:layout_width="70dp"
android:layout_height="40dp"
android:shadowColor="@color/noColor"
android:background="@color/transparentBlack1"
android:text="@string/NewButton"
android:textSize="30sp"
android:gravity="center"
android:textColor="@color/background2"
android:onClick="addLine"/>
</LinearLayout>
使用addLine函数,我只需在布局中添加另一个元素。 有没有办法将按钮保持在布局中每个元素的底部,所以我用代码创建的其他元素将位于按钮上方?
答案 0 :(得分:1)
我猜您使用linearLayout.addView(view);
添加您的观点,对吗?您可以指定视图的添加位置,如下所示:
linearLayout.addView(view, linearLayout.getChildCount() - 1);
这应该在视图和按钮之间添加它。
请参阅文档以获取方法说明:
答案 1 :(得分:1)
尝试在该布局之外设置该按钮,如下所示:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/transparentBlack1"/>
</LinearLayout>
<Button
android:layout_width="70dp"
android:layout_height="40dp"
android:shadowColor="@color/noColor"
android:background="@color/transparentBlack1"
android:text="@string/NewButton"
android:textSize="30sp"
android:gravity="center"
android:textColor="@color/background2"
android:onClick="addLine"/>
</LinearLayout>
希望这有帮助