textView自动调整文本以填充宽度

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

我有一个表格布局,其中有两列文字视图(参见下面的xml)。在小屏幕上,文本填充几乎所有空间,看起来很好。但是在更大的屏幕上会留下大量未使用的空间(更糟糕的是,左列和右列之间存在间隙)。

当有更多屏幕宽度可用时(尤其是横向),使文本大小增加的最佳方法是什么。也许它可以自动完成?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout"
android:background="#E0F9FF"
tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    android:layout_weight="1">
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="12dp"
    android:id="@+id/tableLayout" >
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/tbl_ipv4_ext"
            android:id="@+id/textView"
            android:layout_column="0"
            android:layout_weight="6"
            android:paddingBottom="10dp"
            android:paddingLeft="6dp"
            android:textStyle="bold|italic" />

        <TextView
            android:textIsSelectable="true"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/tbl_no_connection"
            android:id="@+id/textView2"
            android:layout_column="10"
            android:layout_weight="9"
            android:textStyle="bold"/>
    </TableRow>        
<...many similar table rows...>
</TableLayout>
</ScrollView>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="bla-bla-bla" />

android textview scaling
2个回答
1
投票

您可以使用StaticLayout并为您的宽度计算最佳字体大小,或者使用autofitTextView获取一些lib,例如:https://github.com/AndroidDeveloperLB/AutoFitTextView


0
投票

1-在项目gradle文件上导入android支持库26.x.x.如果IDE上没有支持库,则会自动下载。

dependencies {
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:support-v13:26.1.0' 
}

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
  } 
}

2-打开布局XML文件,并像这个标记一样重构TextView。这种情况是:当系统增加字体大小时,将文本适合可用宽度,而不是自动换行。

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/textViewAutoSize"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:ellipsize="none"
        android:text="Auto size text with compatible lower android versions."
        android:textSize="12sp"
        app:autoSizeMaxTextSize="14sp"
        app:autoSizeMinTextSize="4sp"
        app:autoSizeStepGranularity="0.5sp"
        app:autoSizeTextType="uniform" />
© www.soinside.com 2019 - 2024. All rights reserved.