按钮边距的布局问题

21

我在Android应用程序中遇到了布局组织问题。我正在动态创建按钮,并使用此代码将它们添加到我的布局中:

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (int i = 0; i < NO_NUMBERS; i++){

        Button btn = new Button(this);
        btn = (Button) layoutInflater.inflate(R.layout.button, null);
        btn.setId(2000+i);
        Integer randomNumber = sort.getNumbersCopy()[i];
        btn.setText(randomNumber.toString());
        btn.setOnClickListener((OnClickListener) this);
        buttonList.addView(btn);
        list.add(btn);
    }

我正在将它添加到LinearLayout中:

<LinearLayout
    android:id="@+id/buttonlist"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="185dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

</LinearLayout>

我正在导入这个 .xml 文件并在其中定义按钮布局:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="26dp"
android:textStyle ="bold"
android:textColor="#ffffff"
android:background="@drawable/button"
android:layout_marginLeft="8px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>

布局总是变成这样:enter image description here

而不是像这样(按钮之间有空格,按钮是正方形的): enter image description here

总结一下,我需要:

  • 在xml中描述按钮
  • 动态生成N个按钮
  • 将所描述的按钮属性添加到动态创建的按钮中
  • 组织布局,使其可以平均分配buttonList中的按钮,并在它们之间添加空格
3个回答

56

记住,android:layout_*属性是LayoutParams。它们是传递给父视图的参数,并影响父视图对该视图执行布局的方式。您正在为按钮指定layout_margin属性,但它们被忽略了。原因如下:

由于LayoutParams是特定于父视图类型的,因此在使用LayoutInflater填充布局时,需要提供正确类型的父实例,否则布局中顶层视图上的layout_属性将被删除。(Inflater不知道要生成什么类型的LayoutParams。)

由于buttonList是您预期的按钮视图的父级,请将您的inflate行更改为以下内容:

btn = (Button) layoutInflater.inflate(R.layout.button, buttonList, false);

4
嗯,我认为这根本不是问题所在。 - Matthew
好的,我尝试过了。你的解决方案产生了新的问题。在这种情况下,layoutInflater返回buttonList。也许我使用它不正确。如果是这种情况,我该如何在XML中描述按钮布局并将其添加到某个按钮? - Ante
5
抱歉,我在方法调用中漏掉了“false”这个参数 :) 谢谢,问题解决了! - Ante
2
很高兴听到这个消息! :) inflate 的返回值取决于它是否将充气视图附加到第二个参数中指定的父视图上。第三个可选布尔参数指定如果不为 null,则应将充气视图添加到父视图中,它的默认值为 true。(是的,这很烦人,我们很抱歉。 :))参考:http://goo.gl/ak6z8 - adamp
如何为自定义的具体视图执行此操作,就像在原地创建一样: val layout = LinearLayout(context, null, 0, style) 这里忽略了样式边距,尽管视图类型是具体的。 - Kurovsky

2

在按钮本身上设置layout_weight会使按钮在没有空间之间扩展。 LinearLayout从不在其子视图之间添加空格。

您应该将每个按钮包装在FrameLayout中,并在按钮上使用layout_gravity="center",然后在FrameLayout上设置layout_weight="1"

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <Button android:layout_{width,height}="wrap_content"
       android:layout_gravity="center"/>
</FrameLayout>

好吧,“layout_weight”属性是我解决这个问题的最后一次尝试。当然,它失败了。 :) - Ante
你是否使用FrameLayout来包装每个按钮?同时确保按钮实际上有足够的空间来扩展(也许可以去掉LinearLayout上的边距参数)。 - Matthew
在我的 Nexus One 上,屏幕尺寸与此模拟器不同,而且有足够的空间,但结果却相同。这似乎是一个不错的解决方案,但我不知道如何在此之后管理按钮。我需要动态地将它们添加到布局中,在 XML 中描述的属性中填充它们,然后对它们进行一些操作。如果我填充了它们,那么下一步该怎么做呢?请描述如何在此之后使用此布局。 - Ante

0

这也可以通过将TextView嵌入LinearLayout(垂直)中来实现

text_view.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:text="Large Text"
              android:id="@+id/textView"
              android:layout_margin="10dp"
              android:textColor="@color/white"/>

</LinearLayout>

这个被嵌入到另一个视图中,其中根视图也是LinerLayout。 一个复杂的解决方法...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接