Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中,如何根据文本的长度动态调整textview的大小?_Android_Android Layout - Fatal编程技术网

在android中,如何根据文本的长度动态调整textview的大小?

在android中,如何根据文本的长度动态调整textview的大小?,android,android-layout,Android,Android Layout,我从服务器获取可变大小的数据,并将其设置为textView,我希望textView根据文本集的长度调整大小 它在材料设计指南中给出。如何对其进行编码 链接到指南-也许您可以尝试以下方法: EditText et = someEditText; String text = et.getText().toString(); int textLength = text.length(); if (textLength > 25) { et.setTextSize(toSomeNumb

我从服务器获取可变大小的数据,并将其设置为textView,我希望textView根据文本集的长度调整大小

它在材料设计指南中给出。如何对其进行编码


链接到指南-

也许您可以尝试以下方法:

EditText et = someEditText;
String text = et.getText().toString();
int textLength = text.length();

if (textLength > 25) {
    et.setTextSize(toSomeNumber as the size of the text);
} else if (textLength > 15) {
     et.setTextSize(toSomeNumber bigger than the previous);
} else {
      et.setTextSize(toSomeNumber this is the biggest);
}
更新: 如果你想用更为android的方式解决你的问题,你可以参考这个问题


这就是我自己代码的用途。我们接受任何建议来改进我的代码。希望有帮助。

addTextChangedListener是编辑文本的侦听器。 此侦听器监视editText的更改,它有三种不同的状态

EditText edt = someEditText;
edt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        /*watch the editText on every input and changes, then mange the size by if statements and editText length*/
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (edt.getText().toString().length() > 10){
                edt.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall);
            }
            else if (edt.getText().toString().length() > 5){
                edt.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
更新:根据问题,您可以创建组件(自定义视图) 并根据需要从AppCompatTextView名称扩展它; 在其初始化中,您可以添加以下代码:

public class CustomTextView extends AppCompatTextView {
Context ctx;

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    ctx = context;
    init();
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    ctx = context;
    init();
}

public CustomTextView(Context context) {
    super(context);
    ctx = context;
    init();
}

public void init() {
    setOnTouchListener(null);
    addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (getText().toString().length() > 10){
                setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall);
            }
            else if (getText().toString().length() > 5){
                setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}
}


您必须在xml中使用它,而不是通常的文本视图使用下面的库,它非常适合您的需要

根据文档:一个文本视图,自动调整文本大小,使其完全符合其范围。


链接:

现在这个问题有了正式的解决方案。Android O引入的自动调整文本视图可在Support Library 26中获得,并可向后兼容到Android 4.0


支持库的修订版26.0.1增加了对中自动调整大小的支持

开发人员现在可以根据TextView的大小和特性自动扩展或收缩文本大小,从而更容易在不同屏幕或动态内容上优化文本大小

粒度 在Java中:

调用
setAutoSizeTextTypeUniformWithConfiguration()
方法:

setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)
setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
在XML中:

使用autoSizeMinTextSize、AutoSizeTextSize和autoSizeStepGranularity属性设置布局XML文件中的自动调整尺寸:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/autosizing_textview_presetsize" 
        android:layout_width="wrap_content" 
        android:layout_height="250dp" 
        android:layout_marginLeft="0dp" 
        android:layout_marginTop="0dp" 
        app:autoSizeMaxTextSize="100sp" 
        app:autoSizeMinTextSize="12sp" 
        app:autoSizeStepGranularity="2sp" 
        app:autoSizeText="uniform" 
        android:text="Hello World!" 
        android:textSize="100sp" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatTextView 
        android:id="@+id/autosizing_textview_presetsize" 
        android:layout_width="wrap_content" 
        android:layout_height="250dp" 
        android:layout_marginLeft="0dp" 
        android:layout_marginTop="0dp" 
        app:autoSizeText="uniform" 
        app:autoSizePresetSizes="@array/autosize_text_sizes" 
        android:text="Hello World!" 
        android:textSize="100sp" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintTop_toTopOf="parent" />
在XML中:

在布局XML文件中使用autoSizePresetSizes属性:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/autosizing_textview_presetsize" 
        android:layout_width="wrap_content" 
        android:layout_height="250dp" 
        android:layout_marginLeft="0dp" 
        android:layout_marginTop="0dp" 
        app:autoSizeMaxTextSize="100sp" 
        app:autoSizeMinTextSize="12sp" 
        app:autoSizeStepGranularity="2sp" 
        app:autoSizeText="uniform" 
        android:text="Hello World!" 
        android:textSize="100sp" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatTextView 
        android:id="@+id/autosizing_textview_presetsize" 
        android:layout_width="wrap_content" 
        android:layout_height="250dp" 
        android:layout_marginLeft="0dp" 
        android:layout_marginTop="0dp" 
        app:autoSizeText="uniform" 
        app:autoSizePresetSizes="@array/autosize_text_sizes" 
        android:text="Hello World!" 
        android:textSize="100sp" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintTop_toTopOf="parent" />

要将数组作为资源访问,请在res/values/arrays.xml文件中定义数组:

<array name="autosize_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
</array>

10便士
12便士
20便士
40便士
100便士

这里有一种方法,使用支持库:

<androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:breakStrategy="balanced" 
        android:gravity="center_horizontal"
        android:maxLines="1"
        android:text="Hello world" 
        android:textSize="300sp" 
        app:autoSizeTextType="uniform"
        tools:targetApi="o"/>
或者这个:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

请注意,建议在TextView上设置布局限制(宽度和/或高度),以确保正确设置。所有这些都取决于您的用例。

请记住,这会破坏可访问性。例如,如果用户决定通过电话设置更改字体大小,因为他根本看不清楚,高度设置为DP的自动调整文本视图将不会受到影响


为了解决这个问题,您应该在SP中使用
android:layout\u height
,这样当辅助功能设置更改时,它也会被缩放

有什么方法可以在布局本身中完成吗?请在您的文章中添加更多解释answer@IvanBarayev检查您非常想要的TextView和not EditViewautosize+wrap_内容的UpdateThak,不鼓励这些内容,因为它们可能会产生意外的结果:@Kikiwa是有意义的。这只是一个例子。您应该始终具有自动调整大小的功能,并具有某种规则,以便它知道如何自动调整大小。一切都取决于此文本视图所在的布局。+1对于setAutoSizeTextTypeUniformWithConfiguration,虽然文档比较稀疏,但列出的参数非常有用。单位,其中单位是类型值常量之一,即复杂单位或其他单位。