相关疑难解决方法(0)

按钮边距的布局问题

我在android aplication中组织布局有问题.我正在动态创建按钮并将这些代码添加到我的布局中:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

我将它添加到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>
Run Code Online (Sandbox Code Playgroud)

我正在导入这个.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"/>
Run Code Online (Sandbox Code Playgroud)

好吧,布局总是这样结束:在此输入图像描述

而不是像这样的东西(甚至按钮,方形按钮之间的间距): 在此输入图像描述

总结一下:我必须:

  • 在xml中描述按钮
  • 动态生成N个按钮
  • 将描述按钮的属性添加到动态创建的按钮
  • 组织布局,以便它可以在buttonList中均匀分布按钮,并在tham之间留出空格

layout android android-layout

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

android-layout ×1

layout ×1