Layout problem with button margin

后端 未结 3 1352
青春惊慌失措
青春惊慌失措 2020-11-30 10:04

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

    LayoutInflate         


        
3条回答
  •  迷失自我
    2020-11-30 10:45

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

提交回复
热议问题