20

TextViewaを a の中央に配置しようとしてLinearLayoutいますが、水平方向に中央揃えされていますが、垂直方向には中央揃えされていません。

以下は私のコードです

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:background="@drawable/rectblack"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:minWidth="25px"
        android:minHeight="25px">
        <TextView
            android:text="Explode a Vin"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tvExVin" />
    </LinearLayout>

最後に、の左側に垂直に中央揃えにしLinearlayoutたいと思います。誰かがそれを行う方法を理解できれば、それは素晴らしいことです。

ありがとう!

4

3 に答える 3

4

gravityに変更してみてください

android:layout_gravity="center_vertical"

gravityView内にコンテンツを配置するために使用され、親内layout_gravityに を配置するために使用されます。View

しかし、「最終的には Linearlayout の左側に垂直に中央揃えにしたい」と混乱しています。しかし、それが聞こえる方法から、私の答えはあなたが望むものを与えるはずです.

また、よほどの理由がない限りLinearLayout、単一の子に対して や親を使用する必要はありません。必要なもののより良い説明やスクリーンショットを提供できる場合は、より良い解決策を提供できる可能性があります.

これにより、目的の効果が得られます

<LinearLayout
    android:layout_width="320dp"       <!-- removed orientation (horizontal by default)  -->
    android:layout_height="50dp"
    android:background="@drawable/rectblack"
    android:layout_marginTop="10dp"
    android:layout_gravity="center"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Explode a Vin"
        android:layout_gravity="center_vertical"       <!-- changed gravity here  -->
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"           <!-- change to wrap_content -->
        android:id="@+id/tvExVin" />
</LinearLayout>
于 2013-07-10T20:19:57.713 に答える
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AppLock Demo"
        android:textSize="30dp" />

</LinearLayout>
于 2015-07-30T10:24:20.003 に答える