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

问题描述 投票:1回答:2

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

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

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

java android android-layout textview android-screen-support
2个回答
5
投票

更新:

我已根据您的要求开发了尺寸感知TextView。它会在文本大小发生变化时通知侦听器。我测试了它,效果很好。我希望它对你有所帮助。

size aware TextView.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);
    }
}

main 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


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);
            }
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.