java - 多个自动调整大小的 TextView 只有一个大小

标签 java android android-layout textview android-screen-support

我有一个包含不同框的布局,每个框都包含一堆布局相似的 TextView。

我希望使用 TextView 的自动调整大小功能,但每个 TextView 只考虑自己的边界,并且无法在布局中代表相似元素的多个自动调整大小的 TextView 上强制执行相同的大小.

理想情况下,我希望能够“链接”多个 TextView 对象(位于完全不同的位置),因此自动调整大小机制知道它们都应该具有相同的文本大小(坚持最小值,因为一个文本可以比其他的长)。

最佳答案

更新:

我已经为您的要求开发了一个大小敏感的 TextView。当文本大小发生变化时,它会通知监听器。我已经测试过了,效果很好。希望对您有所帮助。

SizeAwareTextView.java:

package com.aminography.textapp;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

public class SizeAwareTextView extends AppCompatTextView {

    private OnTextSizeChangedListener mOnTextSizeChangedListener;
    private float mLastTextSize;

    public SizeAwareTextView(Context context) {
        super(context);
        mLastTextSize = getTextSize();
    }

    public SizeAwareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLastTextSize = getTextSize();
    }

    public SizeAwareTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mLastTextSize = getTextSize();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mLastTextSize != getTextSize()) {
            mLastTextSize = getTextSize();
            if (mOnTextSizeChangedListener != null) {
                mOnTextSizeChangedListener.onTextSizeChanged(this, mLastTextSize);
            }
        }
    }

    public void setOnTextSizeChangedListener(OnTextSizeChangedListener onTextSizeChangedListener) {
        mOnTextSizeChangedListener = onTextSizeChangedListener;
    }

    public interface OnTextSizeChangedListener {

        void onTextSizeChanged(SizeAwareTextView view, float textSize);
    }
}

主 Activity .java

package com.aminography.textapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final SizeAwareTextView textView1 = findViewById(R.id.textView1);
        final SizeAwareTextView textView2 = findViewById(R.id.textView2);
        final SizeAwareTextView textView3 = findViewById(R.id.textView3);

        final List<SizeAwareTextView> textViewList = new ArrayList<>();
        textViewList.add(textView1);
        textViewList.add(textView2);
        textViewList.add(textView3);

        SizeAwareTextView.OnTextSizeChangedListener onTextSizeChangedListener = new SizeAwareTextView.OnTextSizeChangedListener() {
            @SuppressLint("RestrictedApi")
            @Override
            public void onTextSizeChanged(SizeAwareTextView view, float textSize) {
                for (SizeAwareTextView textView : textViewList) {
                    if (!textView.equals(view) && textView.getTextSize() != view.getTextSize()) {
                        textView.setAutoSizeTextTypeUniformWithPresetSizes(new int[]{(int) textSize}, TypedValue.COMPLEX_UNIT_PX);
                    }
                }
            }
        };

        for (SizeAwareTextView textView : textViewList) {
            textView.setOnTextSizeChangedListener(onTextSizeChangedListener);
        }

        ((EditText) findViewById(R.id.editText)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                textView1.setText(editable.toString());
            }
        });
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <com.aminography.textapp.SizeAwareTextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DEDEDE"
        android:text="Here is the first TextView"
        android:textSize="26sp"
        app:autoSizeMinTextSize="10sp"
        app:autoSizeStepGranularity="0.5sp"
        app:autoSizeTextType="uniform" />

    <com.aminography.textapp.SizeAwareTextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="#DEDEDE"
        android:text="Here is the second TextView"
        android:textSize="26sp"
        app:autoSizeMinTextSize="10sp"
        app:autoSizeStepGranularity="0.5sp"
        app:autoSizeTextType="uniform" />

    <com.aminography.textapp.SizeAwareTextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="#DEDEDE"
        android:text="Here is the third TextView"
        android:textSize="26sp"
        app:autoSizeMinTextSize="10sp"
        app:autoSizeStepGranularity="0.5sp"
        app:autoSizeTextType="uniform" />

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Here is the first TextView" />

</LinearLayout>

最终结果:

enter image description here

关于java - 多个自动调整大小的 TextView 只有一个大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52443831/

相关文章:

android - 如果输入集中在导航时,应用程序会崩溃

android - showAsAction ="ifRoom"即使有足够的空间也不显示项目

java - Vector 中的竞争条件

java - 如何以 kb 为单位获取位图的大小

android - 尽管当前短信铃声设置如何,但如何更改 Android 短信铃声?

android - picasso 图像不显示

java - 为什么不调用 keyPressed 方法?

Java - OpenCV 忽略额外的轮廓

android - 应用程序启动时,抽屉导航总是膨胀

Android Activity 指示器?