Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

android - Layout problem with button margin

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);
    }

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>

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:enter image description here

Instead of something like this (even spce between buttons, square buttons): enter image description here

To summarize this: I have to:

  • describe button in xml
  • dynamically generate N buttons
  • add properties of described button to dynamically created ones
  • organize layout so it can evenly distribute buttons in buttonList with spaces between tham
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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.)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...