java - 对于多个自动调整大小的 TextView,只有一种大小
问题描述
我有一个带有不同框的布局,每个框都包含一堆布局相似的 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);
}
}
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());
}
});
}
}
活动主.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>
最后结果:
推荐阅读
- excel - 使用 Excel VBA 自动获取网站图像 URL
- ruby-on-rails - 如何读取哈希数组作为 input.txt 文件以在 ruby 中排序
- java - 批量插入和提取的 JPA/Hibernate 性能改进
- node.js - 如何在后端重新启动 expressjs 应用程序并同时重新加载浏览器选项卡?
- pandas - 如何将前一行的值分配给当前行?
- python - 如何将 Kivy 应用程序连接到数据库
- redhawksdr - 不与域管理器关联的分布式 Redhawk 设备管理器
- apache-kafka - 如果 Apache Kafka 收到 2 MB 消息会发生什么?由于默认大小仅为 1 MB
- python - 将带有int和字典的字符串转换为列表
- node.js - 当一个客户端连接到套接字服务器时如何拥有一个套接字ID