TextSwitcher 的自动调整大小 TextView

我看到 Android 发布了Oreo一个新属性textView

android:autoSizeTextType

这会textView根据它显示的文本字符串调整布局。

我怎样才能将它与 a 一起使用textSwitcher


慕村225694
浏览 133回答 3
3回答

小怪兽爱吃肉

我不知道上面的那些,但这是一个编码最少的解决方案。您可以更改主布局高度以查看自动调整大小,或者您可以点击上部切换器。一个很酷的功能是第二个文本字段的样式在点击时会延续(即文本是白色的)。class MainActivity : AppCompatActivity() {&nbsp; &nbsp; override fun onCreate(savedInstanceState: Bundle?) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main)&nbsp; &nbsp; }&nbsp; &nbsp; fun switchText(v: View) {&nbsp; &nbsp; &nbsp; &nbsp; (v as? TextSwitcher)?.let{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it.tag = (((it.tag as? Int) ?: 0) + 1) % it.childCount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it.setText((it.children.toList()[it.tag as Int] as TextView).text)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}activity_main.xml<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="350dp"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <TextSwitcher&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/a"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@color/purple_200"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@id/b"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintVertical_chainStyle="spread"&nbsp; &nbsp; &nbsp; &nbsp; android:onClick="switchText"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="5dp">&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/a1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="999 / 999"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:letterSpacing="-0.05"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMaxTextSize="936sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMinTextSize="28sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeTextType="uniform"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:lines="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:includeFontPadding="false" />&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/a2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="1000000 / 10000000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/white"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:letterSpacing="-0.05"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMaxTextSize="136sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMinTextSize="28sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeTextType="uniform"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:lines="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:includeFontPadding="false" />&nbsp; &nbsp; </TextSwitcher>&nbsp; &nbsp; <TextSwitcher&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/b"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@color/teal_200"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/a"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintVertical_chainStyle="spread"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="5dp">&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/b1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="999 / 999"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:letterSpacing="-0.05"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMaxTextSize="136sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeMinTextSize="28sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:autoSizeTextType="uniform"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:lines="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:includeFontPadding="false" />&nbsp; &nbsp; </TextSwitcher></androidx.constraintlayout.widget.ConstraintLayout>

绝地无双

&nbsp;<?xml version="1.0" encoding="utf-8"?>&nbsp;<TextView&nbsp; &nbsp;xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp;android:id="@+id/textView"&nbsp; &nbsp;android:layout_width="wrap_content"&nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp;android:text="@string/hello" />&nbsp;private ViewFactory viewFactory = new ViewFactory() {&nbsp; &nbsp; public View makeView()&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);&nbsp; &nbsp; &nbsp; &nbsp; TextView textView = (TextView) inflater.inflate(R.layout.textView, null);&nbsp; &nbsp; &nbsp; &nbsp; return textView;&nbsp; &nbsp; }};

慕姐4208626

&nbsp; altitudeSwitcher = (TextSwitcher) findViewById(R.id.altitude);&nbsp; &nbsp; altitudeSwitcher.setFactory(new ViewFactory() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public View makeView() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView t = new TextView(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return t;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java