如何以编程方式在 constraintLayout 上设置带有 Autosized textType 的 textView?
How to set a textView with Autosized textType on a constraintLayout programmatically?
大家好,我是一个该死的初学者,需要一些帮助
我必须在 Android Studio 的编辑器上构建一个包含 5 个文本视图的 ContraintLAyout,现在我动态需要这个 ContraintLayout 42 次。所以我搜索了一段时间的解决方案并找到了一些可行的方法。但是结果不是预期的。
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="MO"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.34"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.34" />
如何在 Java 代码上设置这些值?
我试过这样的东西
GridLayout gridLayout = findViewById(R.id.kalenderfeldGL);
ConstraintLayout tagesfeld = (ConstraintLayout) gridLayout.getChildAt(0);
tagesfeld.setId(View.generateViewId());
TextView tagname = new TextView(this);
tagname.setId(View.generateViewId());
//tagname.setHeight(0);
//tagname.setWidth(0);
ConstraintLayout.LayoutParams cltagesfele = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT, ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT);
tagname.setLayoutParams(cltagesfele);
//tagname.setText("MO");
TextViewCompat.setAutoSizeTextTypeWithDefaults(tagname, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
tagname.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
//tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
//tagname.setText("MO");
tagesfeld.addView(tagname);
ConstraintSet cstagname = new ConstraintSet();
//cstagname.clone(tagesfeld);
cstagname.connect(tagname.getId(), ConstraintSet.TOP, tagesfeld.getId(), ConstraintSet.TOP, 0);
cstagname.connect(tagname.getId(), ConstraintSet.START, tagesfeld.getId(), ConstraintSet.START, 0);
cstagname.constrainPercentHeight(tagname.getId(), 0.34F);
cstagname.constrainPercentWidth(tagname.getId(), 0.34F);
cstagname.applyTo(tagesfeld);
tagname.setText("MO");
我想要左上角的 "MO" 个字母,但这些字母不是并排的,它们处于垂直位置,并且 O 在下面被切掉了。
尝试将 textView 的最大行限制设置为 1,以防止 "O" 字母进入下一行。如下所示:
tagname.setMaxLines(1);
更新:
尝试使用以下方法减小 autoSizeTextType
的最小文本大小:
xml:
android:autoSizeMinTextSize="10sp"
Java:
tagname.setAutoSizeTextTypeUniformWithConfiguration(10, 100, 1, TypedValue.COMPLEX_UNIT_DIP);
默认值为 12sp
,如 android docs 中所述
.
大家好,我是一个该死的初学者,需要一些帮助
我必须在 Android Studio 的编辑器上构建一个包含 5 个文本视图的 ContraintLAyout,现在我动态需要这个 ContraintLayout 42 次。所以我搜索了一段时间的解决方案并找到了一些可行的方法。但是结果不是预期的。
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="MO"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.34"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.34" />
如何在 Java 代码上设置这些值?
我试过这样的东西
GridLayout gridLayout = findViewById(R.id.kalenderfeldGL);
ConstraintLayout tagesfeld = (ConstraintLayout) gridLayout.getChildAt(0);
tagesfeld.setId(View.generateViewId());
TextView tagname = new TextView(this);
tagname.setId(View.generateViewId());
//tagname.setHeight(0);
//tagname.setWidth(0);
ConstraintLayout.LayoutParams cltagesfele = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT, ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT);
tagname.setLayoutParams(cltagesfele);
//tagname.setText("MO");
TextViewCompat.setAutoSizeTextTypeWithDefaults(tagname, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
tagname.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
//tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
//tagname.setText("MO");
tagesfeld.addView(tagname);
ConstraintSet cstagname = new ConstraintSet();
//cstagname.clone(tagesfeld);
cstagname.connect(tagname.getId(), ConstraintSet.TOP, tagesfeld.getId(), ConstraintSet.TOP, 0);
cstagname.connect(tagname.getId(), ConstraintSet.START, tagesfeld.getId(), ConstraintSet.START, 0);
cstagname.constrainPercentHeight(tagname.getId(), 0.34F);
cstagname.constrainPercentWidth(tagname.getId(), 0.34F);
cstagname.applyTo(tagesfeld);
tagname.setText("MO");
我想要左上角的 "MO" 个字母,但这些字母不是并排的,它们处于垂直位置,并且 O 在下面被切掉了。
尝试将 textView 的最大行限制设置为 1,以防止 "O" 字母进入下一行。如下所示:
tagname.setMaxLines(1);
更新:
尝试使用以下方法减小 autoSizeTextType
的最小文本大小:
xml:
android:autoSizeMinTextSize="10sp"
Java:
tagname.setAutoSizeTextTypeUniformWithConfiguration(10, 100, 1, TypedValue.COMPLEX_UNIT_DIP);
默认值为 12sp
,如 android docs 中所述
.