32

アプリケーションの2行のボタンが他のボタンよりも数ピクセル下にシフトしている理由を理解しようとしています。

エラーダイアログ。 エラーメッセージは

3番目のボタンのテキストを1行に収まるまで短くすると、これは発生しません。これは、改行と関係があることを示しています。ボタンのレイアウトに追加android:layout_gravity="top"しても効果がないようです。これを引き起こしている可能性のあるアイデアはありますか?

編集:レイアウトXMLファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:gravity="center_horizontal"
              android:padding="10dip"
              android:layout_width="wrap_content">

  <TextView android:id="@+id/error_text"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:text="Place holder"
            android:layout_width="wrap_content"
            android:textSize="17dip"/>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_horizontal"
                android:padding="10dip"
                android:layout_width="wrap_content">
    <Button android:id="@+id/ok_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ok"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    <Button android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dip"
            android:text="@string/cancel_login"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:visibility="gone"/>

    <Button android:id="@+id/third_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dip"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:visibility="gone"/>
  </LinearLayout>
</LinearLayout>
4

5 に答える 5

89

水平LinearLayoutは、デフォルトですべての子コントロールのベースラインを揃えます。したがって、複数行ボタンのテキストの最初の行は、他のボタンのテキストの 1 行と垂直方向に揃えられます。

この動作を無効にするには、 に設定android:baselineAligned="false"LinearLayoutます。

于 2011-11-28T00:21:28.927 に答える
4

(デバイス上の)レイアウトを見てみましたが、なぜこの奇妙な動作を示すのかわかりません。レイアウトをデバッグするときは、ビューに背景色を付けて、ビューが占めるスペースをより明確に確認できるようにします。すべてのパディングを削除すると、ボタンが同じトップラインに配置されていないことがわかります。android:gravity="center_vertical"包含に適用するLinearLayoutと、最初の2つのボタンは中央に配置されていますが、最後のボタンは上端にぴったりと収まっています。

RelativeLayoutこれに対する1つの解決策は、 :を使用して内部コンテナを書き直すことです。

<RelativeLayout
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_width="wrap_content">

<Button android:id="@+id/ok_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        android:textColor="@color/black"
        android:textStyle="bold"/>

<Button android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_toRightOf="@id/ok_button"
        android:text="@string/cancel_login"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:visibility="gone"/>

<Button android:id="@+id/third_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_toRightOf="@id/cancel_button"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:visibility="gone"/>
</RelativeLayout>

私のテストから、を使用するRelativeLayoutことにより、すべてのボタンを同じ上端に配置することができます。

于 2011-08-02T21:08:33.847 に答える
2

次の変更を加えてコードを実行したところ、正常に機能しました。水平線形レイアウトの android:gravity を "center_horizo​​ntal" から "center" に変更します。

于 2011-08-02T21:27:56.573 に答える
2

XML の 3 番目のボタンに次の行を追加し、高さを "fill_parent" にしました。

android:paddingTop="4dp"
android:layout_height="fill_parent"

私にとっては、4dp は問題なく動作しました。多かれ少なかれ必要かどうかを確認できます。

これらの変更を行うと、あなたのボタンは仲間のようにうまくいきます:-)

于 2011-08-03T04:53:46.227 に答える
0

3 つのボタンすべてにandroid:layout_height="fill_parent"を記述します。

于 2012-05-19T11:27:26.033 に答える