Remove vertical padding from horizontal ProgressBar

后端 未结 22 2033
眼角桃花
眼角桃花 2020-11-28 03:30

By default the ProgressBar has a certain padding above and below the bar itself. Is there a way to remove this padding so as to only have the bar in the end?

22条回答
  •  情话喂你
    2020-11-28 04:07

    A simple no-tricks solution which is compatible with any version of Android and doesn't need external libraries is faking ProgressBar with two Views inside LinearLayout. This is what I ended up with. Looks pretty neat and this approach is quite flexible - you can animate it in funky ways, add text etc.

    Layout:

    
    
        
    
        
    
    

    Code:

    public void setProgressValue(float percentage) {
        TextView progressValue = (TextView) findViewById(R.id.inventory_progress_value);
        TextView progressRemaining = (TextView) findViewById(R.id.inventory_progress_remaining);
    
        LinearLayout.LayoutParams paramsValue = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams paramsRemaining = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    
        paramsValue.weight = (100 - percentage);
        paramsRemaining.weight = percentage;
    
        progressValue.setLayoutParams(paramsValue);
        progressRemaining.setLayoutParams(paramsRemaining);
    
    }
    

    Result (with some elevation added):

提交回复
热议问题