【问题标题】:Is there a way to make 2 Textviews of different layouts the same height?有没有办法让 2 个不同布局的 Textview 具有相同的高度?
【发布时间】:2015-11-22 05:33:00
【问题描述】:

我在不同的布局文件中有两个文本视图。我希望 textview 1 的高度取决于 textview 2 的高度,这是动态的)。有没有办法以编程方式或在 xml 中执行此操作?

谢谢!

【问题讨论】:

    标签: android xml textview


    【解决方案1】:

    您可以通过编程方式设置 TextView 的高度,如下所示

    LayoutParams params = textView.getLayoutParams();
    params.height = 50;
    textView.setLayoutParams(params);
    

    【讨论】:

      【解决方案2】:

      您可以将addTextChangedListener() 添加到您的secondTextView 以收听文本更改。

      当第二个文本改变时,你使用 LayoutParams 更新第一个的高度。

      例如:

       yourSecondText.addTextChangedListener(new TextWatcher() {
      
                    public void afterTextChanged(Editable s) {  
      
                         LayoutParams secondTextparams = yourSecondText.getLayoutParams();
                         LayoutParams firstTextparams = yourFirsText.getLayoutParams();
      
                         firstTextparams.height = secondTextparams.height;
                         yourFirsText.setLayoutParams(firstTextparams);
                    }
      
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
      
                    public void onTextChanged(CharSequence s, int start, int before, int count) {}
                 });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-08
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        相关资源
        最近更新 更多