【问题标题】:Adjust EditText to font size将 EditText 调整为字体大小
【发布时间】:2012-02-11 10:01:48
【问题描述】:

我想知道如何将编辑文本的高度调整为字体大小。我的意思是无论我将 fontSize 更改为 5sp 还是保留默认值,editText 总是相同的高度。当然我可以使用这样的东西:

<EditText
         android:id="@+id/msgInput"
         android:layout_width="wrap_content"
         android:layout_height="20dp"
         android:inputType="textMultiLine"
         android:textSize="14sp"
         android:hint="@string/hintMsgInput">
</EditText>

但如果有超过 1 行,我也想拉伸。我该怎么做?

[编辑]:

好的,我是这样解决的:

msgInput = (EditText) findViewById(R.id.msgInput);
        msgInput.addTextChangedListener(new TextWatcher()
        {

            public void afterTextChanged(Editable s) {
                if (msgInput.getLineCount() > 1)
                {
                    msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                }
                else
                {
                    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
                    msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, height));
                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {         
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {         
            }

        });

【问题讨论】:

标签: android xml android-edittext


【解决方案1】:

将编辑文本高度设置为 wrap_content

android:layout_height="wrap_content"

【讨论】:

  • 我描述的是当我使用 wrap_content 所以它不是我的解决方案
【解决方案2】:

检查一下

<EditText 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:id="@+id/editText1" 
    android:textSize="5sp"
    android:inputType="textMultiLine"
    android:text="hello h r u hello  h r u hello  h r u hello  h r u hello  h r u "
></EditText>

【讨论】:

    【解决方案3】:

    您正在为 EditText 的高度提供 20 dp 约束。 如果您希望 textView 调整文本大小使用的高度

    android:layout_height="wrap_content"
    

    默认情况下单行是假的。这确保了多行真实。

    【讨论】:

    • 20dp 只是例子,看评论
    【解决方案4】:

    如果你想让 EditText 的高度超过一行,你可以在 EditText 中放入:

    android:layout_height="fill_parent" 并设置 android:layout_weight="1" 以获得更多空间来在活动中添加更多视图

    【讨论】:

    【解决方案5】:

    你可以使用一个滚动视图,并在滚动视图中添加你的editText,然后使用这个:

    android:inputType="textImeMultiLine"
    

    【讨论】:

      【解决方案6】:

      您可以添加@style页面名称

      <style name="value_big" parent="@android:style/TextAppearance.Medium">
        <item name="android:textSize">35sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">sans</item>
        <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
      </style>
      

      在你的编辑文本中调用它

                        <EditText
                          android:id="@+id/msgtext"
                          android:layout_width="fill_parent"
                          style="@style/value_big"
                          android:layout_alignParentLeft="true"
                          android:layout_marginLeft="10dip"
                          android:layout_marginTop="10dip"
                          android:layout_marginRight="10dip"
                          android:inputType="textMultiLine"
                          android:layout_height="150dip"
                          android:gravity="top"
                          android:padding="5dip"> 
                        </EditText>
      

      【讨论】:

        【解决方案7】:

        在editText中设置android:focusable="false"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-17
          • 2012-09-18
          • 1970-01-01
          • 1970-01-01
          • 2011-11-24
          相关资源
          最近更新 更多