Java 以编程方式创建的布局底部的按钮
我做了一个线性布局,在那里我有一个视图和一个按钮。在线性布局中,我也添加了更多的元素,但编程方式不同。代码如下:Java 以编程方式创建的布局底部的按钮,java,android,xml,Java,Android,Xml,我做了一个线性布局,在那里我有一个视图和一个按钮。在线性布局中,我也添加了更多的元素,但编程方式不同。代码如下: <LinearLayout android:id="@+id/container" 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"/>
<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函数,我只需在布局中添加另一个元素。
有没有办法将按钮保持在布局中每个元素的底部,这样我用代码创建的其他元素将位于按钮上方?我猜您使用的是
linearLayout.addView(view)代码>添加视图,对吗?可以指定视图应添加到的位置,如下所示:
linearLayout.addView(view, linearLayout.getChildCount() - 1);
这将在视图和按钮之间添加它
有关方法说明,请参阅文档:
我猜您使用的是linearLayout.addView(视图)代码>添加视图,对吗?可以指定视图应添加到的位置,如下所示:
linearLayout.addView(view, linearLayout.getChildCount() - 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>
希望这有助于尝试将该按钮置于该布局之外,如下所示:
<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>
希望这有帮助