繁体   English   中英

以编程方式创建的布局底部的按钮

[英]Button at bottom of programmatically created layout

我做了一个线性布局,其中有一个视图和一个按钮。 在该线性布局中,我还添加了一些其他元素,但以编程方式。 这是代码:

    <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
        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>

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM