在某些分辨率下,TextView 文字被裁剪了。

18

我有一个包含CardViewTextViewImageView等其他布局的RecyclerView
问题是,在某些屏幕分辨率下,TextView会被截断或挤到下一行,这不是我想要的。
在其他分辨率下,TextView有足够的空间。

小分辨率:
Small

高分辨率:
enter image description here

如何组织布局以便为TextView留出足够的空间,并相应地调整ImageView的大小?

以下是我的RecyclerView项目的xml:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="@+id/zmanCard"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
cardview:cardUseCompatPadding="false"
cardview:cardPreventCornerOverlap="false"
cardview:cardCornerRadius="4dp"
cardview:cardElevation="2dp">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="top"
        android:gravity="center"
        android:orientation="vertical"
        android:background="?attr/colorPrimaryDark">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/zmanCardTitle"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textSize="13sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="24dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="4dp"
            android:alpha="0.8"
            android:id="@+id/zmanCardImage" />
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="4dp">
            <TextView
                android:text="5:40"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:id="@+id/zmanCardTime"
                android:textColor="#ffffff"
                android:textSize="18sp" />
            <ProgressBar
                android:id="@+id/zmanProgressBar"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_gravity="top|left"
                style="@style/Base.Widget.AppCompat.ProgressBar" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>


1
感谢您的悬赏和关注! - Kamil Ibadov
@Charuක 抱歉? - amitairos
3
@amitairos,我给你的堆栈再加一个,尽管它已经溢出了。 - Charuක
@amitairos,我看到现在已经有被接受的答案了,尝试帮助你 :) - Kamil Ibadov
1
@Kamillbadov 哇,谢谢!不过,我没有接受答案的原因是我没有时间尝试所有建议,突然间我被淹没了...至少有Charu的答案。非常感激! - amitairos
显示剩余7条评论
12个回答

13

试试这个。百分之百有效。

AutoResizeTextViewsdp 单位可以帮助你。

SDP - 可伸缩大小单元

一款 Android SDK 提供了新的大小单位 - sdp(可伸缩 dp)。该大小单位会随着屏幕大小的变化而变化,有助于 Android 开发人员支持多个屏幕。

对于文本视图,请参考基于 sp 大小单元的 ssp。

https://github.com/intuit/sdp

自动调整 TextView 文字以适应边界

如果您想为不同的屏幕支持不同的布局设计:

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

了解更多信息请点击这里


3

enter image description here

一个自动调整文本大小以完美适应其边界的TextView。

使用方法:

dependencies {
    compile 'me.grantland:autofittextview:0.2.+'
}

在代码中启用任何扩展TextView的视图:

AutofitHelper.create(textView);

在XML中启用任何继承自TextView的视图:

<me.grantland.widget.AutofitLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        />
</me.grantland.widget.AutofitLayout>

在代码或XML中使用内置小部件:

<RootElement
    xmlns:autofit="http://schemas.android.com/apk/res-auto"
    ...
<me.grantland.widget.AutofitTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:maxLines="2"
    android:textSize="40sp"
    autofit:minTextSize="16sp"
    />

参考链接: https://github.com/grantland/android-autofittextview


3
我可以提出两个建议来解决文本大小问题。第一,使用wrap_content属性,使其适应文本大小;第二,使用配置限定符为不同的屏幕配置提供多种备选布局。您可以使用配置限定符,让运行时根据当前设备的配置(如不同屏幕尺寸的不同布局设计)自动选择适当的资源。支持不同的屏幕尺寸 从这一部分到结尾是我从@ Herry的答案中复制的:此链接。我认为你需要查看这份Google IO设计pdf。在该pdf中转到页码:77,在那里您将找到如何建议为Android的不同设备使用dimens.xml的内容,例如查看下面的结构:

res/values/dimens.xml

res/values-small/dimens.xml

res/values-normal/dimens.xml

res/values-large/dimens.xml

res/values-xlarge/dimens.xml

例如,您已在values中使用了下面的dimens.xml。
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <dimen name="text_size">18sp</dimen>
</resources>

在其他的values文件夹中,您需要更改文本大小的值。
例如:
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="font_size_small">10sp</dimen>
    <dimen name="font_size_medium">20sp</dimen>
    <dimen name="font_size_large">30sp</dimen>
</resources>

点赞这个答案,因为它不仅仅是复制粘贴和演示另一个人的库,这种类型的答案应该得到鼓励! - Charuක

2

如果您想要可扩展的屏幕,尽可能不要使用固定高度或宽度。在您的情况下,应该按比例使用layout_weight来放置组件。

更多细节请参考:

android:layout_weight是什么意思?


如果在某些情况下需要使用比例布局,我们必须在LinearLayout中使用权重,但我更倾向于使用RelativeLayout,它有助于拉伸图像。 - Arpan24x7

2

如果您想更简单地创建布局,可以使用RelativeLayout并运用一些适当的技巧来解决问题。如果仍然有问题,请向我请求 .xml 文件。


2

我认为你不应该固定GridLayoutspanCount。对于高分辨率,最好显示4个项目;对于小分辨率,显示3个项目;对于横向显示,显示5个项目,以此类推...

要做到这一点,您应该在所有支持的values文件夹中创建一个常量,例如:

res/values-small/dimens.xml
      constants.xml
res/values-large/dimens.xml
      constants.xml

对于每个constants.xml,它看起来像这样:

<resources>
<integer name="span_count">3</integer>
</resources>

当您创建GridLayoutManager

GridLayoutManager lm = new GridLayoutManager(Context context, getContext().getResources().getInteger(R.integer.span_count));

做完这个步骤之后,您的问题就不会再发生了。

2

您应该使用padding来确保textview始终具有一些固定的填充,以免被裁剪。 在您的代码中进行以下更改

  <TextView
            android:text="5:40"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:id="@+id/zmanCardTime"
            android:textColor="#ffffff"
            android:padding:"3dp"
            android:textSize="18sp" />

1
使用 AutoScaleTextView AutoScaleTextView 获取 android:textSize 值并将其用作首选文本大小。您还可以设置最小文本大小。默认的最小文本大小为 10dp。现在,AutoScaleTextView 尝试获取首选大小和最小大小之间的最大值,以适应视图的宽度(考虑填充)。
请在下面链接中找到完整的 AutoScaleTextView 类及其实现。

http://ankri.de/autoscale-textview/


1
尝试给TextView布局和ImageView布局分配权重,将权重1分配给两个布局。

1

您又来了,这是第二次使用关键字

分辨率

你现在手头有哪些选择?

总结一下这些答案(全方位!)还有其他选择吗?

As I can see, your problem involves two closely related cases:
  1. Obviously, your text is not resizing. 2. Your layout is too flexible.

    enter image description here

Sometimes TextView itself goes down. Are there any other options to avoid this, instead of the solutions you have?

PercentRelativeLayout

What are the extra advantages of this? Well, it supports percentage-based dimensions and margins. You can set fixed margins and it will give you them using a percentage. It even has a cool attribute called layout_aspectRatio. Here is a link for a small tutorial.

基于视图的比例/绝对位置给出尺寸

在使用PercentRelativeLayout之前,我主要使用这种方法。首先,您需要拥有视图的模型图像/草图。我的模型高度为1920像素,宽度为1080像素(通常是HDTV分辨率,16:9宽高比)。

在该图像中,我们知道视图的位置。

enter image description here

那么对于任何视图/其他屏幕,我可以使用相同的比例来使用LayoutParams 来定位它们。现在视图将根据您的规则保持位置。

这是一个例子:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;

LinearLayout.LayoutParams topLenP= new LinearLayout.LayoutParams(300 * width / 1080, 400 * width / 1920); 
//topLenP.topMargin = x* height / 1920;
//topLenP.leftMargin = y* width / 1080;
myView.setLayoutParams(topLenP);

我在这里所做的实际上是,如我在我的模型视图中所说,此视图需要300宽,400高,因此我正在为我的当前屏幕调整相同的比例。(使用当前屏幕大小)。
回到自动缩放,因为现在我们有一个基于相同比例的视图,这里是另一个好答案。 Android 自适应 TextView android:textAppearance 最后,在Android的\platforms\android-X\data\res\values\themes.xml中根据小、大和中等大小给出了固定的尺寸。
如果没有明确说明您需要什么,您也可以将其用作。
<TextView
   android:textAppearance="?android:attr/textAppearance"
   ....
   /> 

希望这可以帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接