Place 3 buttons in a LinearLayout to occupy equal amount of space

前端 未结 9 1794
滥情空心
滥情空心 2021-01-31 14:50

i would like to have three buttons taking equal amount of available space horizontally in a row. I used android:layout_gravity. What is the problem?

layout

9条回答
  •  感动是毒
    2021-01-31 15:44

    I have make it pragmatically without weight

     float width = CommonUtills.getScreenWidth(activity);
        int cardWidth = (int) CommonUtills.convertDpToPixel(((width) / 3), activity);
    
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(cardWidth,
                LinearLayout.LayoutParams.MATCH_PARENT);
    
        btnOne.setLayoutParams(params);
        btnTwo.setLayoutParams(params);
        btnThree.setLayoutParams(params);
    
        public class CommonUtills {
            public static float getScreenWidth(Context context) {
                float width = (float) 360.0;
                DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                width = displayMetrics.widthPixels / displayMetrics.density;
                return width;
            }
        }
    
    
    
        
    
    
            
    
    
            
    
    
    
             
            
    

提交回复
热议问题