对于多个自动调整大小的TextView,只有一种大小

时间:2018-09-21 12:31:03

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

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

我希望使用TextView的自动调整大小功能,但是每个TextView仅考虑其自身的边界,并且无法在表示布局中相似元素的多个自动调整大小的TextView上强制使用相同的大小

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

4 个答案:

答案 0 :(得分:5)

已更新:

我已经为您的需求开发了尺寸感知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);
    }
}

MainActivity.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

答案 1 :(得分:1)

我对此进行了修改,以便您可以直接从XML使用它,而无需使用Fragment或Activity中的任何代码

您需要将以下代码段添加到values/attrs.xml中:

<declare-styleable name="SizeAwareTextView">
    <attr name="group" format="reference"/>
</declare-styleable>

values/arrays.xml中声明属于同一组的标签ID

<array name="labels">
    <item>@id/label1</item>
    <item>@id/label2</item>
    <item>@id/label3</item>
</array>

然后,在声明视图使用组属性引用标签时:

        <SizeAwareTextView
            android:id="@+id/label1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/white"
            android:textSize="14sp"
            android:textAllCaps="true"
            android:maxLines="1"
            app:autoSizeTextType="uniform"
            app:autoSizeMaxTextSize="15sp"
            app:group="@array/labels"
            android:text="@string/very_long_string"/>

下面是修改后的SizeAwareTextView

class SizeAwareTextView: AppCompatTextView {

    private var lastTextSize: Float = 0F
    private var viewRefs: TypedArray? = null
    private var views = mutableListOf<SizeAwareTextView>()
    var onTextSizeChangedListener: OnTextSizeChangedListener? = object : OnTextSizeChangedListener {
        @SuppressLint("RestrictedApi")
        override fun onTextSizeChanged(view: SizeAwareTextView, textSize: Float) {
            resolveViews()
            views.forEach {
                if (view != it && view.textSize != it.textSize) {
                    it.setAutoSizeTextTypeUniformWithPresetSizes(intArrayOf(textSize.toInt()), TypedValue.COMPLEX_UNIT_PX)
                }
            }
        }
    }

    constructor(context: Context) : super(context) {
        lastTextSize = textSize
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        lastTextSize = textSize
        val a = context.obtainStyledAttributes(attrs, R.styleable.SizeAwareTextView)
        a.getResourceId(R.styleable.SizeAwareTextView_group, 0).let {
            if (it > 0) {
                viewRefs = resources.obtainTypedArray(it)
            }
        }
        a.recycle()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        lastTextSize = textSize
        val a = context.obtainStyledAttributes(attrs, R.styleable.SizeAwareTextView)
        a.getResourceId(R.styleable.SizeAwareTextView_group, 0).let {
            if (it > 0) {
                viewRefs = resources.obtainTypedArray(it)
            }
        }
        a.recycle()
    }

    fun resolveViews() {
        viewRefs?.let {
            var root = parent
            while (root.parent is View) {
                root = root.parent
            }
            for (i in 0 until it.length()) {
                val resId = it.getResourceId(i, 0)
                val v = (root as View).findViewById<SizeAwareTextView>(resId)
                if (v != null) {
                    views.add(v)
                } else {
                    Log.w(TAG, "Resource: $resId not found at idx: $i")
                }
            }
            it.recycle()
            viewRefs = null
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (lastTextSize != textSize) {
            lastTextSize = textSize
            onTextSizeChangedListener?.onTextSizeChanged(this, lastTextSize)
        }
    }

    interface OnTextSizeChangedListener {
        fun onTextSizeChanged(view: SizeAwareTextView, textSize: Float)
    }

    companion object {
        val TAG = SizeAwareTextView::class.java.simpleName
    }
}

答案 2 :(得分:0)

(我认为)这与OP可能要寻找的有所不同,但是一旦确定布局的大小后,我需要的是包含多个TextView对象的特定视图。最小的TextView成为所有TextView的大小。因此,我将其放入了TextViews所在片段的OnViewCreated()方法中:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Timber.d("Lifecycle: In onViewCreated() of WelcomeFragment adjusting text fields");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                //noinspection deprecation
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            // Get all text views and find the smallest size and set them all to that
            int childCount = ((ViewGroup)view).getChildCount();
            float f = -1;
            ArrayList<AppCompatTextView> textViewArrayList = new ArrayList();
            for (int x = 0; x < childCount; x++) {
                View v = ((ViewGroup) view).getChildAt(x);
                if ( v instanceof androidx.appcompat.widget.AppCompatTextView) {
                    textViewArrayList.add((androidx.appcompat.widget.AppCompatTextView)v);
                    if ( f == -1) {
                        // Handle edge case - first TextView found initializes f
                        f = Math.max(f, ((androidx.appcompat.widget.AppCompatTextView) v).getTextSize());
                    } else {
                        f = Math.min(f, ((androidx.appcompat.widget.AppCompatTextView) v).getTextSize());
                    }
                }
            }
            int[] uniformSize = new int[]{(int) f};
            for (int x = 0; x < textViewArrayList.size(); x++) {
                TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(textViewArrayList.get(x), uniformSize, TypedValue.COMPLEX_UNIT_PX);
            }
        }
    });
}

答案 3 :(得分:0)

以下是基于氨基造影的答案的 kotlin 解决方案:

SizeAwareTextView.kt

package com.example.onesizemultipleauto_sizetextviews

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

class SizeAwareTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    var onTextSizeChangedListener: OnTextSizeChangedListener? = null
    private var lastTextSize: Float = textSize

    @Override
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (lastTextSize != textSize) {
            lastTextSize = textSize
            if (onTextSizeChangedListener != null) {
                onTextSizeChangedListener?.onTextSizeChanged(this, lastTextSize)
            }
        }
    }

    interface OnTextSizeChangedListener {
        fun onTextSizeChanged(view: SizeAwareTextView, textSize: Float)
    }
}

MainActivity.kt

package com.example.onesizemultipleauto_sizetextviews

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue

import android.annotation.SuppressLint
import com.example.onesizemultipleauto_sizetextviews.SizeAwareTextView.OnTextSizeChangedListener


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val walletUnlocksDescription: SizeAwareTextView = findViewById(R.id.walletUnlocksDescription)
        val walletMinutesDescription: SizeAwareTextView = findViewById(R.id.walletMinutesDescription)

        val textViewList = listOf(walletUnlocksDescription, walletMinutesDescription)

        val onTextSizeChangedListener: OnTextSizeChangedListener =
            object : OnTextSizeChangedListener {
                @SuppressLint("RestrictedApi")
                override fun onTextSizeChanged(view: SizeAwareTextView, textSize: Float) {
                    for (textView in textViewList) {
                        if (textView != view && textView.textSize != view.textSize) {
                            textView.setAutoSizeTextTypeUniformWithPresetSizes(
                                intArrayOf(textSize.toInt()),
                                TypedValue.COMPLEX_UNIT_PX
                            )
                        }
                    }
                }
            }

        textViewList.forEach { sizeAwareTextView ->
            sizeAwareTextView.onTextSizeChangedListener = onTextSizeChangedListener
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.example.onesizemultipleauto_sizetextviews.SizeAwareTextView
        android:id="@+id/walletUnlocksDescription"
        android:layout_width="0dp"
        android:layout_height="18dp"
        android:text="This is a long text that autosizes ok S"
        android:autoSizeTextType="uniform"
        android:gravity="start|top"
        android:textAlignment="viewStart"
        android:textSize="18sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="@id/minutesGuideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="@id/unlocksGuideline"
        app:layout_constraintBottom_toBottomOf="parent" />

    <com.example.onesizemultipleauto_sizetextviews.SizeAwareTextView
        android:id="@+id/walletMinutesDescription"
        android:layout_width="0dp"
        android:layout_height="18dp"
        android:layout_marginEnd="44dp"
        android:gravity="start|top"
        android:textAlignment="viewStart"
        android:textSize="18sp"
        android:autoSizeTextType="uniform"
        android:text="Short text"
        app:layout_constraintBaseline_toBaselineOf="@id/walletUnlocksDescription"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/minutesGuideline"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/unlocksGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="110dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/minutesGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="100dp" />

</androidx.constraintlayout.widget.ConstraintLayout>