2012-05-16 10 views
5

私はLinearLayoutに2つのボタンとTextViewを持つアクティビティを持っています。 My TextViewは下方向にオフセットされており、テキストはボックス内に収まらない。何が起きているのか説明できますか?私はそれがパディングに関連していると思うし、TextViewパディングの危険についていくつかの議論を読んだことがあるが、それはなぜテキストが最下部でカットされているのか説明していない。Android TextViewオフセットが下にあり

<?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" 
    android:background="#800080"> 

    <Button 
     android:text="This" 
     android:background="@drawable/button_red" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <Button 
     android:text="That" 
     android:background="@drawable/button_green" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <TextView 
     android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#533f93" 
    /> 
</LinearLayout> 

このコードは、この表示を生成する:

User interface

パープルのLinearLayoutであり、青のTextViewあります。ご覧のように、TextViewの上部はボタンの上部にあり、下部はLinearLayoutの下部にあります。 TextViewにテキストを追加すると、LinearLayoutの高さは適切に増加しますが、TextViewがオフセットされているため、最後の行の下端は常に失われます。

私は階層ビューアを実行し、それが私にこのワイヤーフレームました:上部の垂直オフセットを示しているが、のTextViewの下をミス

Wireframe image from Hierarchy Viewer

を。このようなのLinearLayout選択ルックスと同じワイヤーフレーム:階層ビューア、ボタンの上部によると

Wireframe image from Hierarchy Viewer - LinearLayout selected

は0であるが、TextViewののトップが7である私は、様々な修正を試してみましたほとんどの場合、このサイトから選択されました:

android:paddingTop="0dp" 
    android:background="@null" 
    android:includeFontPadding="false" 

これらの問題は修正されていません。

答えて

5

android:baselineAlignedあなたのLinearLayoutからfalseのプロパティを設定します。ドキュメントから

に設定され、その子の ベースラインを揃えるからレイアウトを防ぐことができます。この属性は、子供が重力に対して異なる値を使用する場合に特に便利です。デフォルト値はです。です。

+0

はい、うまくいきます! LinearLayoutがその境界線を越えてテキストをプッシュするのがなぜデフォルトであるのか分かりません。 – TomDestry

+1

たとえば、この投稿を読んで[ベースラインの平均](https://groups.google.com/d/topic/android-developers/1gPy9zo28ak/discussion)の内容を理解できます。ですから、デフォルトでは、 'LinearLayout'の後続のウィジェットは、その前のウィジェットとベースラインで整列しています。私たちの場合、** TextViewの最初の**行は、 'Button'のテキストにベースラインで整列されています。私は同意する、それは直感的な行動ではないが、それはそうである。 – StenaviN

0

は、このようなTextViewのためlayout_gravityを設定してみてください:

<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
    android:layout_gravity="top" 
/> 
+0

それは動作しません – StenaviN

2

あなたは、いくつかのTextViewsでこの問題を持っている場合は、追加することができますcenter_vertical

<?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" 
android:background="#800080"> 

<Button 
    android:text="This" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<Button 
    android:text="That" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
android:layout_gravity="center_vertical"/> 
</LinearLayout> 
+0

ガー!だから簡単に、ありがとう!デフォルトはNONEのようです。なぜデフォルトではテキストが部分的に見えなくなりますか? – TomDestry

0

するのTextViewのlayout_gravityを与えますandroid:gravity="center_vertical"LinearLayout

関連する問題