布局问题按钮保证金

布局问题按钮保证金

本文介绍了布局问题按钮保证金的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,在安卓aplication组织布局。我动态创建按钮并将其添加这个code到我的布局:

I have problem with organizing layout in android aplication. I'm dynamically creating buttons and adding them with this code to my layout:

    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:

I'm adding it to the 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在那里我定义按键布局:

and i'm importing this .xml where i'm defining button layout:

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

嗯,布局最终总是这样的:

Well, layout always ends up like this:

,而不是像这样(按钮之间甚至SPCE,方形按钮):

Instead of something like this (even spce between buttons, square buttons):

要总结:我有:

  • 描述XML按钮
  • 动态产生N个按钮
  • 描述按钮添加属性,动态创建的那些
  • 在组织布局,因此它可以均匀地分配按钮buttonList与艾德里安之间的空格

推荐答案

记住,安卓布局_ * 属性的LayoutParams 。他们争论的父母和会影响家长会的这一观点进行布局。你指定 layout_margin 在你的按钮属性,但他们得到的忽略。这里的原因:

Remember, android:layout_* attributes are LayoutParams. They are arguments to the parent and affect how the parent will perform layout on that view. You're specifying layout_margin attributes on your buttons, but they're getting ignored. Here's why:

由于的LayoutParams 是特定父视图类型,您需要提供正确的父类的一个实例,当您使用 LayoutInflater膨胀布局或者布局_ 在布局的顶层视图属性将被丢弃。 (充气器将不知道是什么类型的的LayoutParams 的生成。)

Since LayoutParams are specific to the parent view type, you need to supply an instance of the correct parent type when you inflate layouts using a LayoutInflater or else layout_ attributes on the top-level view in the layout will be dropped. (The inflater would have no idea what type of LayoutParams to generate.)

由于 buttonList 是你的目标父为按钮的观点,改变你的膨胀行这样的:

Since buttonList is your intended parent for the button views, change your inflate line to this:

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

这篇关于布局问题按钮保证金的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:05