Android按钮编程

Android按钮编程,android,layout,button,Android,Layout,Button,我有以下XML结构 <LinearLayout android:id="@+id/tagsTable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:orientation="vertical">

我有以下XML结构

            <LinearLayout android:id="@+id/tagsTable"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_margin="10dip" android:orientation="vertical">
                <LinearLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                    <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:background="@drawable/btn_white_add_9"
                        android:text="Tag" android:layout_margin="2px"></Button>
                    <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:background="@drawable/btn_white_add_9"
                        android:text="Tag" android:layout_margin="2px"></Button>
                </LinearLayout>
                <LinearLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                    <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:background="@drawable/btn_white_add_9"
                        android:text="Tag" android:layout_margin="2px"></Button>
                    <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:background="@drawable/btn_white_add_9"
                        android:text="Tag" android:layout_margin="2px"></Button>
                </LinearLayout>
                <LinearLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                    <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:background="@drawable/btn_white_add_9"
                        android:text="This Tag is very big that it has full width :)" android:layout_margin="2px"
                        android:paddingLeft="5px" android:paddingRight="25px"></Button>
                </LinearLayout>
            </LinearLayout>

突出的问题是按钮的垂直间距和大小,这两种情况下都会发生变化。XML的方式是完美的。我在代码中做错了什么?

在xml中,您有:

<LinearLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content">

为什么不直接使用XML呢?如果需要在行时间确定行,只需不断地反复加载XML就行了?不行。这个问题无法解决。我正在使用MarginLayoutParams。这不应该是一个问题。在您的代码中编写:MarginLayoutParams params=new MarginLayoutParams(LayoutParams.FILL\u PARENT,LayoutParams.WRAP\u CONTENT);但您从未使用过它,因此您的currentRow未设置为其宽度等于fill_parent。你必须在某个地方做一件事:currentRow;setLayoutParams(参数);好的,我试过了,现在有了,但效果还是一样。感觉需要设置垂直对齐。我注意到另一件事,setPadding在按钮上也不起作用。为什么会出现这种行为?是否在XML布局中自动设置布局权重?
<LinearLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
currentRow.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);