5

LinearLayoutに2つのボタンとTextViewを持つアクティビティがあります。TextViewが下向きにオフセットされており、テキストがボックス内に収まりません。何が起こっているのか説明できますか?これはパディングに関連していると思います。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>

このコードは次の表示を生成します:

ユーザーインターフェース

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

Hierarchy Viewerを実行すると、次のワイヤーフレームが表示されます。

階層ビューアーからのワイヤーフレーム画像

これは上部に垂直オフセットを示していますが、TextViewの下部を見逃しています。LinearLayoutが選択された同じワイヤーフレームは次のようになります。

HierarchyViewerからのワイヤーフレーム画像-LinearLayoutが選択されました

Hierarchy Viewerによると、ボタンの上部は0ですが、TextViewの上部は7です。私はさまざまな修正を試しましたが、ほとんどがこのサイトから抜粋したものです。

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

これらのどれも私の問題を修正しませんでした。

4

4 に答える 4

5

android:baselineAlignedのプロパティをに設定LinearLayoutしますfalse

ドキュメントから:

falseに設定すると、レイアウトが子のベースラインを揃えることができなくなります。この属性は、子供が重力に異なる値を使用する場合に特に役立ちます。デフォルト値はtrueです。

于 2012-05-16T03:54:28.970 に答える
2

Textviewのlayout_gravityを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>
于 2012-05-16T04:53:07.210 に答える
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"
/>
于 2012-05-16T03:56:02.347 に答える
0

複数のTextViewでこの問題が発生した場合は、に追加できandroid:gravity="center_vertical"ますLinearLayout

于 2016-02-18T08:07:33.730 に答える