首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按钮边距的布局问题

按钮边距的布局问题
EN

Stack Overflow用户
提问于 2011-03-16 01:25:01
回答 3查看 18.6K关注 0票数 20

我在组织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"/>

布局总是这样结束的:

而不是像这样的东西(甚至按钮之间的空格,方形按钮):

总结一下:我必须:

xml

  • dynamically中的
  • describe button生成N个按钮
  • 将所描述按钮的属性添加到动态创建的布局中,因此它可以在buttonList中均匀分布按钮,并在

之间留出空格

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-16 03:22:03

请记住,android:layout_*属性是LayoutParams。它们是父级的参数,并影响父级如何在该视图上执行布局。您在按钮上指定了layout_margin属性,但它们被忽略了。原因如下:

由于LayoutParams特定于父视图类型,因此当您使用LayoutInflater膨胀布局时,需要提供正确父类型的实例,否则布局中顶级视图上的layout_属性将被丢弃。(充气装置不知道要生成哪种类型的LayoutParams。)

由于buttonList是按钮视图的预期父对象,因此将inflate行更改为:

btn = (Button) layoutInflater.inflate(R.layout.button, buttonList, false);
票数 56
EN

Stack Overflow用户

发布于 2011-03-16 01:35:00

在按钮本身上设置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>
票数 2
EN

Stack Overflow用户

发布于 2013-08-27 05:28:20

这也是通过将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。一种复杂的变通方法...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5315529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档