Android:文本视图的最后一行被切断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11243867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Last line of textview cut off
提问by Rynardt
I have a horizontal LinearLayout
containing a TextView
followed by a Spinner
next to it. This LinearLayout
is dynamically inflated multiple times in a fixed vertical LinearLayout
contained within a RelativeLayout
.
我有一个水平LinearLayout
包含一个TextView
后面跟着一个Spinner
。这LinearLayout
是在LinearLayout
包含在RelativeLayout
.
The problem is that since I switched from Theme.light
to Theme.holo.light
, the last line of the TextView
gets cut in half. This happens when the dynamic text is long and spans more than one row.
问题是,自从我从 切换Theme.light
到 后Theme.holo.light
,最后一行TextView
被切成两半。当动态文本很长并且跨越多行时会发生这种情况。
I have been able to fix this by adding bottom padding to the horizontal LinearLayout
containing the TextView
and Spinner
.
我已经能够通过向LinearLayout
包含TextView
和的水平添加底部填充来解决这个问题Spinner
。
This does not feel like a fix, but more of a hack. Can someone please give me some advice on how to properly fix this?
这感觉不像是一个修复,而更像是一个黑客。有人可以就如何正确解决此问题给我一些建议吗?
I have also read some other questions, but none seem to help.
我还阅读了其他一些问题,但似乎都没有帮助。
Horizontal Linear layout:
水平线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="TextView"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Relative layout where above layout is dynamically inflated at Linear Layout with id ll2_7:
相对布局,上面的布局在线性布局中动态膨胀,id 为 ll2_7:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/relLayoutButtonNext"
android:layout_below="@id/textView1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:text="2.7" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toRightOf="@id/textView10"
android:text="@string/question2_7" />
<LinearLayout
android:id="@+id/ll2_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView11"
android:layout_below="@+id/textView11"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
EDIT:Here is the complete layout xml for above:
编辑:这是上面的完整布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
style="@style/question_section_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/question2_header" />
<RelativeLayout
android:id="@+id/relLayoutButtonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/bottomBar"
android:paddingBottom="3dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="3dp" >
<Button
android:id="@+id/buttonNext"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="nextStep"
android:text="Next Section"
android:textSize="20sp" />
<Button
android:id="@+id/buttonPrevious"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="previousStep"
android:text="Previous Section"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/relLayoutButtonNext"
android:layout_below="@id/textView1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:text="2.7" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toRightOf="@id/textView10"
android:text="@string/question2_7" />
<LinearLayout
android:id="@+id/ll2_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView11"
android:layout_below="@+id/textView11"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView10"
android:layout_below="@+id/ll2_7"
android:text="2.8" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll2_7"
android:layout_toRightOf="@+id/textView10"
android:text="@string/question2_8" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="@+id/ll2_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginBottom="20dp"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/ll2_8"
android:text="2.9" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll2_8"
android:layout_toRightOf="@+id/textView10"
android:text="@string/question2_9" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="@+id/ll2_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView5"
android:layout_toRightOf="@+id/textView10"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/ll2_9"
android:text="2.10" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll2_9"
android:layout_toRightOf="@+id/textView10"
android:text="@string/question2_10" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="@+id/ll2_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView7"
android:layout_marginBottom="20dp"
android:layout_toRightOf="@+id/textView10"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/ll2_10"
android:text="2.11" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll2_10"
android:layout_toRightOf="@+id/textView10"
android:text="@string/quesiton2_11" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="@+id/ll2_11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView9"
android:layout_below="@+id/textView9"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView8"
android:layout_below="@+id/ll2_11"
android:text="2.11.1" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll2_11"
android:layout_toRightOf="@+id/textView10"
android:text="@string/question2_11_1" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="@+id/ll2_11_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView13"
android:layout_toRightOf="@+id/textView10"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
采纳答案by Jusid
I've encountered the same cut-off issue as shown at the screenshot. It is caused by the baseline alignment in the horizontal LinearLayout
. TextView
and Spinner
have different baselines due to font size difference. To fix the issue it is needed to disable baseline alignment for the layout by setting:
我遇到了与屏幕截图所示相同的截止问题。它是由水平方向的基线对齐引起的LinearLayout
。TextView
并且Spinner
由于字体大小不同而具有不同的基线。要解决此问题,需要通过设置禁用布局的基线对齐:
android:baselineAligned="false"
or in the code:
或在代码中:
layout.setBaselineAligned(false);
回答by Rynardt
After trying a million different things, I think I have the answer.
在尝试了一百万种不同的事情之后,我想我有了答案。
I applied a LayoutGravity
to the TextView item:
我将 a 应用于LayoutGravity
TextView 项目:
android:layout_gravity="fill"
Seems to solve all clipping issues I had. Hope this helps someone with the same problem.
似乎解决了我遇到的所有剪辑问题。希望这可以帮助有同样问题的人。
回答by Neal Sanche
I had the same problem, and found that simply adding
我遇到了同样的问题,发现只需添加
android:includeFontPadding="false"
the final line of text no longer had its descenders clipped.
文本的最后一行不再被剪裁。
回答by Ameer
I added some dummy space after text by adding
我通过添加在文本后添加了一些虚拟空间
textView.setText(firstString+"\n");
I tried all other solution.But this was the only solution worked for me
我尝试了所有其他解决方案。但这是唯一对我有用的解决方案
回答by Rubin Yoo
Put the problematic textview inside a framelayout. I think the text view is not calculated correctly because of the sibling view, Spinner.
将有问题的 textview 放在框架布局中。我认为由于兄弟视图 Spinner,文本视图计算不正确。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="150dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="TextView"/>
</FrameLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
回答by Display name
I found a different solution by extending TextView and adding a custom Class like this:
我通过扩展 TextView 并添加这样的自定义类找到了不同的解决方案:
public class AdaptingTextView extends TextView {
public AdaptingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public AdaptingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AdaptingTextView(Context context) {
super(context);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// set fitting lines to prevent cut text
int fittingLines = h / this.getLineHeight();
if (fittingLines > 0) {
this.setLines(fittingLines);
}
}
}
回答by Spookysister
My solution was close to the accepted one, but I had to change it to
我的解决方案接近接受的解决方案,但我不得不将其更改为
android:layout_gravity="fill_vertical"
instead. Otherwise the other rows would have been stretch as well with added line breaks at random places. For example, the biggest row had 4 lines, so another row was changed from
反而。否则其他行也会被拉伸,并在随机位置添加换行符。例如,最大的一行有 4 行,所以另一行从
this is a testphrase
this is a testphrase
to
到
thi
s is
a testph
rase
回答by Graeme
When this occurs, you should ensure that the TextView
is not growing larger than it's container -
发生这种情况时,您应该确保它的TextView
大小不会超过它的容器 -
If a TextView
is set to wrap_content
and it's container (or an ancestor container) doesn't leave room for the TextView
to grow into it can be occluded.
如果 aTextView
设置为wrap_content
并且它的容器(或祖先容器)没有TextView
为它的生长留出空间可以被遮挡。
If that's not the case, it's also possible the onMeasure()
of the TextView
sometimes doesn't correctly measure the tails of letters, non-latin characters or the effects from text being italic. You can correct for this by setting a global style for your TextView
so it will be picked up without needed to change your entire code base:
如果不是的话,它也可以在onMeasure()
的TextView
有时不能正确测量的字母,非拉丁字符或文本是斜体效果的尾巴。您可以通过为您设置全局样式来纠正此问题,TextView
以便无需更改整个代码库即可获取它:
Ensure that you're application/activities use a custom theme like so:
确保您的应用程序/活动使用自定义主题,如下所示:
<style name="Custom" parent="@android:style/Theme.Light">
<item name="android:textViewStyle">@style/Custom.Widget.TextView</item>
</style>
<style name="Custom.Widget.TextView" parent="@android:style/Widget.TextView">
<item name="android:gravity">fill</item>
<item name="android:padding">1sp</item>
</style>
The answer by @Rynadt was really helpful in getting to the above stage. Setting the gravity of the Text inside the View ensures on some devices that occlusion never takes place (The text is correctly fitted inside the view), on others a helping hand with padding of an sp value, ensures that the tails et al are accounted for with a TextSize specific value.
@Rynadt 的回答对进入上述阶段非常有帮助。在视图内设置文本的重力可确保在某些设备上永远不会发生遮挡(文本正确安装在视图内),在其他设备上使用 sp 值填充帮助确保尾部等具有 TextSize 特定值。
回答by Dheeresh Singh
try with removing android:paddingBottom="20dp"
尝试删除 android:paddingBottom="20dp"
from
从
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
回答by mp501
getViewTreeObserver().addOnGlobalLayoutListener
does not work in a recycler view. If you're using a recycler, use View.addOnLayoutChangeListener
:
getViewTreeObserver().addOnGlobalLayoutListener
在回收站视图中不起作用。如果您使用的是回收器,请使用View.addOnLayoutChangeListener
:
I found that the ellipsizing I defined for textView
in xml was not always reflected so I programmatically set it before reassigning the text property. This worked for me.
我发现我textView
在 xml 中定义的省略号并不总是反映,所以我在重新分配 text 属性之前以编程方式设置它。这对我有用。
textView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
textView.removeOnLayoutChangeListener(this);
float lineHeight = textView.getLineHeight();
int maxLines = (int) (textView.getHeight() / lineHeight);
if (textView.getLineCount() != maxLines) {
textView.setLines(maxLines);
textView.setEllipsize(TextUtils.TruncateAt.END);
// Re-assign text to ensure ellipsize is performed correctly.
textView.setText(model.getText());
}
}
});