Expand TextView with wrap_content until the neighbor view reaches the end of the parent

后端 未结 7 658
遇见更好的自我
遇见更好的自我 2020-12-13 14:26

I need to achieve the following layout:

\"enter

I have two TextViews in a rela

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 14:33

    I set the maxwidth of left textview base of the width of right textview, take a looking my code. hope this will help.

    TextView leftText ;
    TextView rightText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        leftText = (TextView) findViewById(R.id.leftTextView);
        rightText = (TextView) findViewById(R.id.rightTextView);
    
    
        measureView(leftText);
        measureView(rightText);
    
        leftText.setMaxWidth(getWidth() - rightText.getMeasuredWidth());
    
    }
    
    private void measureView(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        int childWidth = ViewGroup.getChildMeasureSpec(0, view.getPaddingLeft() + view.getPaddingRight(), params.width);
        int childHeight = ViewGroup.getChildMeasureSpec(0, view.getPaddingBottom() + view.getPaddingTop(), params.height);
        view.measure(childWidth, childHeight);
    
    }
    
    private int getWidth() {
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getWidth();
    }
    

    and the xml is:

    
    
    
    
    
    
    
    

    this is the screenshot after running: enter image description here

提交回复
热议问题