将EditText调整为字体大小

问题描述 投票:5回答:7

我想知道如何将编辑文本的高度调整为te字体大小。我的意思是,无论我将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
7个回答
3
投票

检查一下

<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
投票

将编辑文本高度设置为wrap_content

android:layout_height="wrap_content"

2
投票

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

android:layout_height="wrap_content"

另外默认情况下,单行为false。这确保多行真实。


0
投票

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

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


0
投票

你可以使用scrollView,并在ScrollView中添加你的editText,并使用:

android:inputType="textImeMultiLine"

0
投票

您可以添加@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>

-1
投票

在editText中设置android:focusable =“false”

© www.soinside.com 2019 - 2024. All rights reserved.